Search code examples
objective-cnscopying

NSCopying, copyWithZone and NSDictionary


Firstly I would like confirmation that I have understood NSCopying correctly ...

In order to use a simple NSObject subclass as a key in an NSDictionary I must have it implement the NSCopying protocol. In the copied instance's copyWithZone method I must alloc/init a new instance of my class, set its properties to be identical to the copied instance and return it.

Secondly, why does an NSDictionary use a copy of the instance added to it rather than the instance itself?


Solution

  • The subclass does not need to implement NSCopying if it is the object, the key should usually be NSStrings, which are copied.