3

I am transcribing some C++ code into ObjC. The operator==() that I am looking at compares its private members one-by-one and returns true iff they are all true.

What is the equivalent implementation of operator==() in ObjC?

From browsing, I see in ObjC:

  • -isEqual
  • -isEqualTo
  • -isLike
  • == (built-in? not overrideable?)
  • any others I missed?

What is the protocol for these methods? (deep or shallow comparison, etc...?) Which one should I be using to replace an operator==() that compares all its members?

kfmfe04
  • 14,936
  • 14
  • 74
  • 140
  • `==` will only compare 2 object's pointer. As simple as that, and you can't override it. You will have to create your own method to compare your own classes. – TheAmateurProgrammer Oct 23 '12 at 10:00
  • 1
    Some discussion of `isEqual:` and `hash` here: http://stackoverflow.com/questions/1112373/implementing-hash-isequal-isequalto-for-objective-c-collections – Monolo Oct 23 '12 at 10:05
  • @Monolo +1 for that useful link - ty – kfmfe04 Oct 23 '12 at 11:13

1 Answers1

4

isEqual: is likely what you want to override. Also ensure that hash returns the same value for two objects that are equal.

There isn't really a protocol for comparison. Do whatever makes sense for your class. If you're comparing all members in your operator==() then that'd be deep comparison and yes, use isEqual:.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • ty - accepted - too bad that there is no protocol for comparison - having so many different ones can definitely cause ambiguity/confusion. – kfmfe04 Oct 23 '12 at 11:14