<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Implementing NSCopying (or NSCopyObject() considered harmful)</title>
	<atom:link href="http://robnapier.net/blog/implementing-nscopying-439/feed" rel="self" type="application/rss+xml" />
	<link>http://robnapier.net/blog/implementing-nscopying-439</link>
	<description>Mac and iPhone, on the brain</description>
	<lastBuildDate>Thu, 02 Feb 2012 16:51:07 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Chris Hanson</title>
		<link>http://robnapier.net/blog/implementing-nscopying-439/comment-page-1#comment-1346</link>
		<dc:creator>Chris Hanson</dc:creator>
		<pubDate>Wed, 28 Apr 2010 00:09:35 +0000</pubDate>
		<guid isPermaLink="false">http://robnapier.net/blog/?p=439#comment-1346</guid>
		<description>&lt;p&gt;An immutable value class with no mutable subclass can implement NSCopying by returning [self retain]; client code should not be able to tell the difference.  That&#039;s what it means to be a value class: equivalent instances are interchangeable, whether or not they&#039;re pointer-equal (so you may as well make them pointer-equal).&lt;/p&gt;

&lt;p&gt;When choosing whether to specify retain or copy for a property, you should not be considering size, time, etc. - you should be considering semantics first and foremost.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>An immutable value class with no mutable subclass can implement NSCopying by returning [self retain]; client code should not be able to tell the difference.  That&#8217;s what it means to be a value class: equivalent instances are interchangeable, whether or not they&#8217;re pointer-equal (so you may as well make them pointer-equal).</p>

<p>When choosing whether to specify retain or copy for a property, you should not be considering size, time, etc. &#8211; you should be considering semantics first and foremost.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Napier</title>
		<link>http://robnapier.net/blog/implementing-nscopying-439/comment-page-1#comment-1340</link>
		<dc:creator>Rob Napier</dc:creator>
		<pubDate>Mon, 26 Apr 2010 17:31:37 +0000</pubDate>
		<guid isPermaLink="false">http://robnapier.net/blog/?p=439#comment-1340</guid>
		<description>&lt;p&gt;The answer you link to has is mostly correct but has made the point too general in my opinion. It is true that public NSString properties should be copied rather than retained, but this is because NSString has a common mutable subclass (NSMutableString). For immutable classes with no mutable subclass, it is generally best to retain rather than copy for memory and performance reasons.&lt;/p&gt;

&lt;p&gt;Yes, it is conceivable that a custom immutable class will later have an immutable class added, but you have to balance this hypothetical against the cost. If you&#039;re very concerned about it, you could add an &lt;code&gt;isMemberOfClass:&lt;/code&gt; assertion, but in my opinion this goes beyond defensive programming to unhelpful paranoia.&lt;/p&gt;

&lt;p&gt;When choosing whether to retain or copy, you must also consider the size of the object. If the object is quite small, then perhaps the cost of copy is worth it. If it is large or set very often, then copying may simply be too inefficient. As an example from the other direction, when returning an NSArray that you store internally as an NSMutableArray, you should make a copy so that the array doesn&#039;t change behind your caller&#039;s back. Unfortunately this can sometimes be a massive performance bottleneck, and I&#039;ve been forced to just return the NSMutableArray cast as an NSArray. Read the docs for &lt;a href=&quot;http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/subviews&quot; rel=&quot;nofollow&quot;&gt;NSView -subviews&lt;/a&gt; for an example where Apple has had to do this, too.&lt;/p&gt;

&lt;p&gt;Always remember to keep the actual goal in mind, and then apply the rules of thumb appropriately.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The answer you link to has is mostly correct but has made the point too general in my opinion. It is true that public NSString properties should be copied rather than retained, but this is because NSString has a common mutable subclass (NSMutableString). For immutable classes with no mutable subclass, it is generally best to retain rather than copy for memory and performance reasons.</p>

<p>Yes, it is conceivable that a custom immutable class will later have an immutable class added, but you have to balance this hypothetical against the cost. If you&#8217;re very concerned about it, you could add an <code>isMemberOfClass:</code> assertion, but in my opinion this goes beyond defensive programming to unhelpful paranoia.</p>

<p>When choosing whether to retain or copy, you must also consider the size of the object. If the object is quite small, then perhaps the cost of copy is worth it. If it is large or set very often, then copying may simply be too inefficient. As an example from the other direction, when returning an NSArray that you store internally as an NSMutableArray, you should make a copy so that the array doesn&#8217;t change behind your caller&#8217;s back. Unfortunately this can sometimes be a massive performance bottleneck, and I&#8217;ve been forced to just return the NSMutableArray cast as an NSArray. Read the docs for <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/subviews" rel="nofollow">NSView -subviews</a> for an example where Apple has had to do this, too.</p>

<p>Always remember to keep the actual goal in mind, and then apply the rules of thumb appropriately.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://robnapier.net/blog/implementing-nscopying-439/comment-page-1#comment-1312</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Fri, 23 Apr 2010 14:42:13 +0000</pubDate>
		<guid isPermaLink="false">http://robnapier.net/blog/?p=439#comment-1312</guid>
		<description>&lt;p&gt;Nice work!&lt;/p&gt;

&lt;p&gt;You wrote &quot;[S]hould a copy of foo have a second pointer to bar, or should it have its own copy of whatever bar points to? There is no way to answer this question generally; it depends on the nature of the objects.&quot;&lt;/p&gt;

&lt;p&gt;Indeed, I suspect I&#039;m wrestling with this issue right now. (I could also be suffering from a bit of developer myopia and need to step back a bit.)&lt;/p&gt;

&lt;p&gt;As I&#039;ve been taught, for attributes whose type is an immutable value class that conforms to the NSCopying protocol, &lt;a href=&quot;http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain/388002#388002&quot; title=&quot;Advice from Apple&#039;s very own Chris Hanson&quot; rel=&quot;nofollow&quot;&gt;it&#039;s wise to specify copy vs retain&lt;/a&gt; in the @property declaration.&lt;/p&gt;

&lt;p&gt;In particular, the recommendation is to &lt;i&gt;always&lt;/i&gt; use copy on NSString setters, which I continue to do to this day:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;code&gt;@property (nonatomic, copy) NSString *bar;
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;However, wouldn&#039;t that go against the advisement - when NSCopying is honored - to &quot;[implement] the accessors with the correct behavior (retain versus copy)&quot;?&lt;/p&gt;

&lt;p&gt;In the case of NSString, is the use of copy in setters a bad idea? Is it a toss-up (&quot;it doesn&#039;t matter&quot;)?&lt;/p&gt;

&lt;p&gt;The urging to use copy on NSString setters is very strong out there in the wild, so I want to make sure I &quot;measure twice, cut once&quot; here!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Nice work!</p>

<p>You wrote &#8220;[S]hould a copy of foo have a second pointer to bar, or should it have its own copy of whatever bar points to? There is no way to answer this question generally; it depends on the nature of the objects.&#8221;</p>

<p>Indeed, I suspect I&#8217;m wrestling with this issue right now. (I could also be suffering from a bit of developer myopia and need to step back a bit.)</p>

<p>As I&#8217;ve been taught, for attributes whose type is an immutable value class that conforms to the NSCopying protocol, <a href="http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain/388002#388002" title="Advice from Apple's very own Chris Hanson" rel="nofollow">it&#8217;s wise to specify copy vs retain</a> in the @property declaration.</p>

<p>In particular, the recommendation is to <i>always</i> use copy on NSString setters, which I continue to do to this day:</p>

<p><pre><code>@property (nonatomic, copy) NSString *bar;
</code></pre></p>

<p>However, wouldn&#8217;t that go against the advisement &#8211; when NSCopying is honored &#8211; to &#8220;[implement] the accessors with the correct behavior (retain versus copy)&#8221;?</p>

<p>In the case of NSString, is the use of copy in setters a bad idea? Is it a toss-up (&#8220;it doesn&#8217;t matter&#8221;)?</p>

<p>The urging to use copy on NSString setters is very strong out there in the wild, so I want to make sure I &#8220;measure twice, cut once&#8221; here!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Napier</title>
		<link>http://robnapier.net/blog/implementing-nscopying-439/comment-page-1#comment-1230</link>
		<dc:creator>Rob Napier</dc:creator>
		<pubDate>Sun, 11 Apr 2010 21:25:35 +0000</pubDate>
		<guid isPermaLink="false">http://robnapier.net/blog/?p=439#comment-1230</guid>
		<description>&lt;p&gt;@ozgur Wow... I even have the extra bracket in there. I should always cut-and-paste code into the blog. Thanks. Fixed.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@ozgur Wow&#8230; I even have the extra bracket in there. I should always cut-and-paste code into the blog. Thanks. Fixed.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Ozgur</title>
		<link>http://robnapier.net/blog/implementing-nscopying-439/comment-page-1#comment-1229</link>
		<dc:creator>Ozgur</dc:creator>
		<pubDate>Sun, 11 Apr 2010 21:07:31 +0000</pubDate>
		<guid isPermaLink="false">http://robnapier.net/blog/?p=439#comment-1229</guid>
		<description>&lt;p&gt;You missed initializing the allocated memory:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;code&gt;Product *copy = [[[self class] allocWithZone: zone]];
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;should be
    Product *copy = [[[self class] allocWithZone: zone]init];&lt;/p&gt;

&lt;p&gt;Thanks for the enlightening article, I was having hard time understanding the drawback of NSCopyObject on the reference manual.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>You missed initializing the allocated memory:</p>

<p><pre><code>Product *copy = [[[self class] allocWithZone: zone]];
</code></pre></p>

<p>should be
    Product *copy = [[[self class] allocWithZone: zone]init];</p>

<p>Thanks for the enlightening article, I was having hard time understanding the drawback of NSCopyObject on the reference manual.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Simo</title>
		<link>http://robnapier.net/blog/implementing-nscopying-439/comment-page-1#comment-593</link>
		<dc:creator>Simo</dc:creator>
		<pubDate>Sun, 22 Nov 2009 15:42:47 +0000</pubDate>
		<guid isPermaLink="false">http://robnapier.net/blog/?p=439#comment-593</guid>
		<description>&lt;p&gt;Hi! Great info.&lt;/p&gt;

&lt;p&gt;Could you post detailed example about the alternative way, this would help iPhone newbies.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi! Great info.</p>

<p>Could you post detailed example about the alternative way, this would help iPhone newbies.</p>]]></content:encoded>
	</item>
</channel>
</rss>

