<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>&#60;&#60; shiftedbits &#187; Cocoa Yups and Nopes</title>
	<atom:link href="http://shiftedbits.org/category/cocoa-yups-and-nopes/feed/" rel="self" type="application/rss+xml" />
	<link>http://shiftedbits.org</link>
	<description>Shifty shifty shifty. Shifty.</description>
	<lastBuildDate>Sun, 29 Jan 2012 23:40:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NSCopying and Mutability</title>
		<link>http://shiftedbits.org/2009/04/17/nscopying-and-mutability/</link>
		<comments>http://shiftedbits.org/2009/04/17/nscopying-and-mutability/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 19:43:12 +0000</pubDate>
		<dc:creator>Devin Lane</dc:creator>
				<category><![CDATA[Cocoa Yups and Nopes]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[macosx]]></category>
		<category><![CDATA[objc]]></category>

		<guid isPermaLink="false">http://shiftedbits.org/?p=121</guid>
		<description><![CDATA[In the Cocoa frameworks, it is common to find mutable and immutable versions of classes that store data, such as strings, dictionaries, and arrays. Most of these classes also implement the NSCopying informal protocol, and those with mutable varients implement the NSMutableCopying protocol. These protocols specify methods for obtaining immutable and mutable copies of an object. These copying methods should be implemented such that the copy can return an instance of a subclass, to allow for subclasses to copy their &#8230; <a href="http://shiftedbits.org/2009/04/17/nscopying-and-mutability/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the Cocoa frameworks, it is common to find mutable and immutable versions of classes that store data, such as strings, dictionaries, and arrays. Most of these classes also implement the <code>NSCopying</code> informal protocol, and those with mutable varients implement the <code>NSMutableCopying</code> protocol. These protocols specify methods for obtaining immutable and mutable copies of an object. These copying methods should be implemented such that the copy can return an instance of a subclass, to allow for subclasses to copy their specific instance variables.</p>
<p>There are situations, however, that can result in the inability of a subclass to do so. Consider <code>NSURLRequest</code>:</p>
<p><pre>
@interface NSURLRequest : NSObject &amp;lt;NSCoding, NSCopying, NSMutableCopying&amp;gt;
</pre></p>
<p>An immutable class, but one that implements <code>NSMutableCopying</code>, it responds directly to the <code>-mutableCopyWithZone:(NSZone *)zone</code> method that returns a mutable url request, an instance of <code>NSMutableURLRequest</code>. While this isn&#8217;t a problem in itself, it becomes an issue when this method is not re-implemented in <code>NSMutableURLRequest</code>.</p>
<div class="yupnoperap">
<p class="nope"><strong>– Nope: </strong>Implement the <code>NSMutableCopying</code> protocol only in your immutable class.</p>
<div class="why">
<p>The implementation of <code>-mutableCopyWithZone:(NSZone *)zone</code> in the immutable class must certainly hard-code the name of the mutable class &#8212; it cannot be aware of any external subclasses of the mutable class. This means that asking for a mutable copy of the immutable class creates an instance of the known mutable subclass. Consider the following example:</p>
<p><pre>
@interface MyMutableURLRequest : NSMutableURLRequest {} @end

MyMutableURLRequest *request = [MyMutableURLRequest requestWithURL:url];
request = [request mutableCopy];
</pre></p>
<p>In this case, the object returned from <code>-mutableCopy</code><strong> will be an instance of <code>NSMutableURLRequest</code></strong> instead of the expected <code>MyMutableURLRequest</code>.
</div>
</div>
<div class="yupnoperap">
<p class="nope"><strong>– Nope: </strong>Hardcode class names in <code>-copyWithZone:</code> and <code>-mutableCopyWithZone:</code></p>
<div class="why">
<p>Hardcoding class names serves to block subclassers from subclassing the copy methods without dirty hacks.</p>
</div>
</div>
<div class="yupnoperap">
<p class="yup"><strong>+ Yup: </strong>[Re]Implement the <code>NSMutableCopying</code> protocol in your mutable class.</p>
<div class="why">
<p>By doing this, the class name doesn&#8217;t have to be hard-coded (in the mutable class), and so a subclasser can simply call <code>[super mutableCopy]</code> and then copy their specific instance variables.</p>
</div>
</div>
<div class="yupnoperap">
<p class="nope"><strong>+ Yup: </strong>Use <code>[self class]</code> in <code>-copyWithZone:</code> and <code>-mutableCopyWithZone:</code></p>
<div class="why">
<p>By using [self class], an object of the subclass type will be allocated.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://shiftedbits.org/2009/04/17/nscopying-and-mutability/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.194 seconds -->

