How do I know which Foundation classes implement custom isEqual and not just inherit NSObject implementation?

For example, how can I be sure that UILocalNotification isEqual: result is not just result of comparing them by memory address?
Should I rely on assumption that every class has custom isEqual implementation? Or should I treat every class like a not supporting isEqual if they don't have special description about isEqual in documentation?

Accepted Answer

You can assume that if a subclass of NSObject has a better notion of what it is to be equal, then it implements an override of isEqual:. However, you don't know when that is, and the question of what equality is, in general, doesn't make sense without context in your code.


The problem is that even if you know what isEqual means for a class, you still have to know why you're testing. For example, we know that NSArray's isEqual represent memberwise isEqual-ity of its elements. But it's right to use pointer equality for NSArray objects sometimes.


I can't see any obvious meaning for value equality for UILocalNotification. Two instances that have identical properties probably still represent separate notifications from the user's point of view. I'd expect isEqual to be the NSObject default in this case.


All of which comes back to a basic question: what are you really trying to do?

Thank you for your answer.


There is no context. I am just wondering if there is a best practice to use isEqual:. I understand that this method used by containers, but it also may be helpfull in tests.


I used the sample with UILocationNotification because my colleague wants to use isEqual: in tests (when you schedule notifications and then check their parameters). I noticed, that it could work, but we rely on undocumented behavior here, so it does not seem like a good way for me. Futhermore, "isEqual:"'s implementation may change from iOS version to version.

> I used the sample with UILocationNotification [...]


UILocationNotification is a particularly interesting case because local notifications get persisted to disk. The last time I looked at UILocationNotification it had an internal UUID that it used to track each notifications through various stages of its lifecycle, and -isEqual: was keyed off that.


Share and Enjoy

--

Quinn "The Eskimo!"

Apple Developer Relations, Developer Technical Support, Core OS/Hardware

Thank you very much for the clarification!

How do I know which Foundation classes implement custom isEqual and not just inherit NSObject implementation?
 
 
Q