Is NSCopyObject considered harmful?

In the Xcode documentation for NSCopyObject the special considerations section reads:

This feature is dangerous and very difficult to use. It is used as part of copyWithZone: any class that may be a subclass that is highly error prone. In the GC section or when using Objective-C 2.0, the zone is completely ignored.

This feature is likely to be deprecated after Mac OS X 10.6.

Why is it difficult to use correctly? It performs a shallow (bit-bit-bit) copy. Objects are not copied or saved. The documentation is pretty straightforward.

If I have no other reasons, what is the preferred alternative for making a shallow copy of an object?

Edit:

There are good reasons why you need to make a shallow copy. One example: a class can have many instance variables, most of which are primitive types (integer, float) or objects that are not intentionally saved to avoid save cycles (delegates). A shallow copy using NSCopyObject copies all of this into one beautiful, self-documenting line of code. Any remaining ivars that need to be counted can be saved or copied individually.

An alternative to this is either assigning to a new object using the pointer syntax ( newObject->ivar = ivar ), or creating an init method with a potentially large number of arguments (one for each ivar to copy). The latter seems to me especially ugly, but I suppose he does not need to be in the headline and expose the world.

+4
source share
2 answers
+2
source

You should not make a shallow copy unrelated to the correct hold / release. Period.

+1
source

All Articles