Question

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?

Was it helpful?

Solution

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

OTHER TIPS

  1. You have to implement NSCopying protocol for every custom class if you want make them copyable
  2. It must use a copy because if you will modify an object contained in the original dictionary it's copy won't be affected by the change
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top