I think that you have discovered a big gap between documentation and reality.
NSCopying protocol documentation requirements:
A returned copy is immutable if the consideration of the issue of “immutable and mutable” refers to the receiving entity; otherwise, the exact nature of the copy is determined by the class.
But this is clearly wrong in some cases, as you showed in your examples (and I sent them feedback about this through this documentation page).
But (# 2), in my opinion, it really doesn't matter, and you don't care.
The -copy point is that it will return an object that you can use, with the guarantee that it will behave independently of the original. This means that if you have a mutable object, -copy it and modify the original object, the copy will not see the effect. (In some cases, I think this means that -copy can be optimized so that it does nothing, because if the object is immutable, it cannot be changed in the first place. Maybe I'm wrong. (Now I wonder what the consequences are for dictionary keys because of this, but what is a separate topic ...))
As you saw, in some cases the new object may actually have a mutable class (even if the documentation tells us that it will not). But as long as you do not rely on the fact that it is changed (why should you?), It does not matter.
What should you do? Always treat the -copy result as immutable , as simple as this.
jtbandes
source share