I would like to use a custom object as a key in a hash-like structure. I’ve tried using NS[Mutable]Dictionary but in order for my object to be a key it has to implement the NSCopying protocol. NSDictionary is sending a copy message to all of it’s keys as far as I’ve read. I don’t want to implement the protocol (my object is quite complex) nor do I want it to be copied. What are my options? Do I have any?
I would like to use a custom object as a key in a hash-like
Share
NSDictionaryis toll-free bridged withCFDictionaryRef, but they actually differ in behavior when adding objects. Specifically,NSDictionary‘s-setObject:forKey:will copy the key, butCFDictionaryRef‘sCFDictionarySetValue()will not copy the key. This means that if you want to use non-copyable keys, you can useCFDictionarySetValue()instead to add it to the dictionary.This will still retain the key, but it won’t copy it. And you can use the normal
NSDictionarymethods for everything else.