RealmObject Equatable redundant message
Asked Answered
M

3

8

We have a simple class Person which inherent from realms Object. Now we want that subclass to conform to the Equatable protocol. The very simple code looks like this.

class Person: Object, Equatable {

    dynamic var localID     = "0"
    dynamic var name:String?
}

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

    return lhs.localID == rhs.localID
}

We started with realm version 0.98.8 and everything worked as aspected. When we updated to version 0.102.0 (and the other versions between) the complier error message occurs

Error: Redundant conformance of ‚Person‘ to protocol ‚Equatable‘

Not strange enough, if downgrade back to version 0.98.8 the error still remains. Another strange behavior, on one of our developers machine, the same code compiles just fine.

After some research we have no idea what is going on and how to fix or workaround this.

Monecious answered 10/5, 2016 at 6:48 Comment(1)
I'm having this exact issue! Thanks for posting this question. I was getting redundant conformance from implementing Hashable. Some of my teammates were fine and others were getting the Error.Leafstalk
A
13

Latest version of RealmSwift implements Equatable by default, you can look at Object.swift from RealmSwift code.

To override default Equatable behaviour, you can override this function:

public override func isEqual(object: AnyObject?) -> Bool

After that, existing Swift code with == will return result based on custom condition defined inside isEqual. No need to create func == manually.

It's still using isEqual due to RLMObjectBase subclassed from NSObject, not pure Swift object.

Antilog answered 10/5, 2016 at 9:48 Comment(0)
A
4

The updated signature for Swift 4 is:

open override func isEqual(_ object: Any?) -> Bool {
    return true
}
Awoke answered 8/3, 2018 at 19:33 Comment(0)
V
0

In RealmSwift, Object is already conforming to Equatable. So you don't have to add the Equatable in the definition of Person.

But you do not seem to be the only one having problems with this.

Veracity answered 10/5, 2016 at 7:11 Comment(1)
This was our fist suggestion. But if we remove the Equatable protocol from the Person class, the equatable check never get called. Also the Realm Object inherits from RLMObjectBase and RLMObjectBase from NSObject. There is no conformance to the Equatable at allMonecious

© 2022 - 2024 — McMap. All rights reserved.