<?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>zaphCoder &#187; BestPractice</title>
	<atom:link href="http://zaph.com/blog/?feed=rss2&#038;tag=bestpractice" rel="self" type="application/rss+xml" />
	<link>http://zaph.com/blog</link>
	<description>All About Code</description>
	<lastBuildDate>Fri, 19 Aug 2016 18:02:31 +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>Fast Enumeration</title>
		<link>http://zaph.com/blog/?p=476</link>
		<comments>http://zaph.com/blog/?p=476#comments</comments>
		<pubDate>Thu, 23 Feb 2012 14:14:50 +0000</pubDate>
		<dc:creator>zaph</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[BestPractice]]></category>

		<guid isPermaLink="false">http://zaph.com/blog/?p=476</guid>
		<description><![CDATA[I was looking at some client code and saw essentially this: NSArray *items = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil]; MyClass *myClass = [[MyClass alloc] init]; for (int i=0; i&#60;4; i++) {     myClass.thing = @”test”;     myClass.widget &#8230; <a href="http://zaph.com/blog/?p=476">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="z-content">
<div class="z-font">
<div id="z-objc">
I was looking at some client code and saw essentially this:</p>
<p class="p4"><span class="s3">NSArray</span><span class="s1"> *items = [</span><span class="s3">NSArray</span><span class="s1"> </span><span class="s4">arrayWithObjects</span><span class="s1">:</span>@"one"<span class="s1">, </span>@"two"<span class="s1">, </span>@"three"<span class="s1">, </span>@"four"<span class="s1">, </span><span class="s2">nil</span><span class="s1">];</span></p>
<p class="p1"><span class="s5">MyClass</span> *myClass = [[<span class="s5">MyClass</span> <span class="s4">alloc</span>] <span class="s4">init</span>];</p>
<p class="p1"><span class="s2">for</span> (<span class="s2">int</span> i=<span class="s5">0</span>; i&lt;<span class="s5">4</span>; i++) {</p>
<p class="p1"><span>    </span>myClass.thing = @”test”;</p>
<p class="p1"><span>    </span>myClass.<span class="s5">widget</span> = [items <span class="s4">objectAtIndex</span>:i];</p>
<p class="p1"><span>    </span><span class="s7">// …</span></p>
<p class="p1">}</p>
<p>There are several issues, among them:</p>
<ul>
<li>In the <span class="s4">for</span> loop the termination value is hard coded, if the number of items in the <span class="s1">NSArray</span> constant are changed the code will break. This could have be averted by using items.<span class="s1">count</span></li>
<li>The indexing into the <span class="s1">NSArray</span> is not close to the declaration of the index (&#8220;i&#8221;) and is easily missed.</li>
<li>It is not clear if the index (&#8220;i&#8221;) is used elsewhere in the loop.</li>
</ul>
<p>Fast enumeration could be used eliminating the above three issues.</p>
<p>Fast enumeration example:</p>
<p class="p4"><span class="s3">NSArray</span><span class="s1"> *items = [</span><span class="s3">NSArray</span><span class="s1"> </span><span class="s4">arrayWithObjects</span><span class="s1">:</span>@"one"<span class="s1">, </span>@"two"<span class="s1">, </span>@"three"<span class="s1">, </span>@"four"<span class="s1">, </span><span class="s2">nil</span><span class="s1">];</span></p>
<p class="p1"><span class="s8">MyClass</span> *myClass = [[<span class="s8">MyClass</span> <span class="s4">alloc</span>] <span class="s4">init</span>];</p>
<p class="p1"><span class="s2">for</span> (<span class="s3">NSString</span> *item <span class="s2">in</span> items) {</p>
<p class="p1"><span>    </span>myClass.<span class="s8">widget</span> = item;</p>
<p class="p1"><span>    </span>myClass.thing = @”test”;</p>
<p class="p1"><span>    </span><span class="s9">// …</span></p>
<p class="p1">}</p>
<p>No lines of code are eliminated, that is not the point, what is important is that the code is future-proofed and is more clear.
</p></div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://zaph.com/blog/?feed=rss2&#038;p=476</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
