fa jo
fa jo

Reputation: 23

RLMObjects not recognised as the same in Realm Cocoa

I have a tableView containing RLMObjects and want to search for the row containing a specific RLMObject. After casting the RLMResult object to its original type it is not the same as its original object:

    // ... adding a todoA with !isCompleted to defaultRealm()
    var firstItem = Todo.objectsWhere("isCompleted == false")[0] as! ToDo
    if firstItem == todoA {
         // todoA is != firstItem even-though they should be the same object
    }

How can I compare two RLMObjects without having to implement the primaryKey allocation?

Upvotes: 2

Views: 386

Answers (1)

jpsim
jpsim

Reputation: 14409

RLMObject does not conform to Swift's Equatable protocol, allowing == and != comparisons. Depending on the equality semantics you want for your objects, you can use the following extension on RLMObject:

extension RLMObject: Equatable {}
func == <T: RLMObject>(lhs: T, rhs: T) -> Bool {
  return lhs.isEqualToObject(rhs)
}

Upvotes: 3

Related Questions