Apple’s docs currently DO NOT DOCUMENT NSSet’s concept of “identity”.
I have some bugs that appear to come from Apple’s code. For instance, “[NSMutableSet minusSet]” never works for me as documented – but I’m pretty sure it’s because of “identity”.
containsObject:
Returns a Boolean value that indicates whether a given object is
present in the set.YES if anObject is present in the set, otherwise NO.
What does that MEAN?
FYI things I’ve tried:
- implemented “isEqual:” on all classes in the set
- checked that all classes are the same class (no subclass / superclass confusion)
- implementd NSCopying on all classes in the set (no effect)
In Cocoa, object equality is done by using
isEqual:andhash:https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html
From the notes for
isEqual::Your subclasses will need to implement both of these, so that they return the same thing. Once they do this, then they can be used correctly in Cocoa Collections.
The reason your
NSSetequality wouldn’t work, is because sets use hashes (it’s stored as a hash table), hence if you only implementedisEqual:, then theres a chance (a good chance) that their hashes would be different.