1

I m getting error when setting up equatable in swift

extension PFObject : Equatable {}

public func ==(lhs:PFObject,rhs:PFObject) -> Bool {

return lhs.objectId == rhs.objectId
}

Below the following error I m getting

Redundant conformance of 'PFObject' to protocol 'Equatable'

Is there any solution for this error?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Vimlan.G
  • 193
  • 1
  • 13

3 Answers3

3

A change in Swift 2 makes every NSObject conform to Equatable through the isEqual implementation which simply compares pointer values (read more here).

You can use the following extension to override the default implementation of isEqual:

extension PFObject {
  public override func isEqual(object: AnyObject?) -> Bool {
    if (object as? PFObject)?.objectId == self.objectId {
      return true
    } else {
      return super.isEqual(object)
    }
  }
}
Ben-G
  • 4,996
  • 27
  • 33
  • It might be a good idea to also compare the types since I believe `objectId` is not necessarily unique across different subclasses of `PFObject`. – Ben Packard Jul 31 '18 at 16:20
0

PFObject inherits from NSObject so it is already conforming to Equatable.

Instead of adding Equatable to a class that already implements it, you should subclass it and override the default implementation.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

Here's an example implementation of isEqual that also checks the object is of the same Parse object type.

extension PFObject {
    open override func isEqual(_ object: Any?) -> Bool {
        if let parseObject = object as? PFObject {
            return parseObject.parseClassName == self.parseClassName && parseObject.objectId == self.objectId
        } else {
            return super.isEqual(object)
        }
    }
}
Ben Packard
  • 26,102
  • 25
  • 102
  • 183