Note that there are some explanatory texts on larger screens.

plurals
  1. PONSCopyObject considered harmful?
    primarykey
    data
    text
    <p>In the Xcode documentation for <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSCopyObject" rel="nofollow noreferrer"><code>NSCopyObject</code></a>, the special considerations section states:</p> <blockquote> <p>This function is dangerous and very difficult to use correctly. It's use as part of <code>copyWithZone:</code> by any class that can be subclassed, is highly error prone. Under GC or when using Objective-C 2.0, the zone is completely ignored.</p> <p>This function is likely to be deprecated after Mac OS X 10.6.</p> </blockquote> <p>Why is it difficult to use correctly? It performs a shallow (bit-for-bit) copy. Objects are not copied or retained. The documentation is pretty clear on this.</p> <p>If I'm not missing other reasons, what is the preferred alternative for performing a shallow copy of an object?</p> <p><strong>Edit:</strong></p> <p>There are valid reasons to want to perform a shallow copy. One example: a class may have many instance variables, most of which are primitive types (integer, float) or objects that are intentionally not retained to avoid retain cycles (delegates). A shallow copy using <code>NSCopyObject</code> copies all of these in one nice, self-documenting line of code. Any remaining ivars that do need to be reference counted can be retained or copied individually.</p> <p>The alternative to this is to either assign to the new object using pointer syntax (<code>newObject-&gt;ivar = ivar</code>) or to create an init method with a potentially large number of arguments (one for each ivar to copy). The latter strikes me as especially ugly, but I suppose it doesn't need to be in the header and exposed to the world.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload