I have the need to uses custom class that I wrote as a key in an NSMutableDictionary (A Socket class, in this case.)
In Java when I use a HashMap or TreeMap, the object requires either a hash function or comparator function respectively for the dictionary to operate. In general, dictionaries need to know some way to compare keys.
From what I’ve read in the documentation, it doesn’t look like I have to do this to use NSMutableDictionary though. Is that correct? And if it is, does anyone know how they implemented key comparison?
As long as the key conforms to
NSCopying(whichNSObjectdoes not, but many things do), it can be used as a key. The key will be copied usingcopyWithZone:.Regarding how this works, it works just as you note in Java. “The object requires either a hash function or comparator function respectively for the dictionary to operate.” Every
NSObjectimplements bothhashandisEqual:.