<?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>Tristan O'Tierney &#187; code</title>
	<atom:link href="http://www.otierney.net/articles/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.otierney.net</link>
	<description>Mac Geek</description>
	<lastBuildDate>Wed, 09 Sep 2009 17:23:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Objective-C Rocks Your Socks</title>
		<link>http://www.otierney.net/articles/2007/01/20/objective-c-rocks-your-socks/</link>
		<comments>http://www.otierney.net/articles/2007/01/20/objective-c-rocks-your-socks/#comments</comments>
		<pubDate>Sat, 20 Jan 2007 20:20:49 +0000</pubDate>
		<dc:creator>tristan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[macdev]]></category>
		<category><![CDATA[objectivec]]></category>

		<guid isPermaLink="false">http://www.otierney.net/articles/2007/01/20/objective-c-rocks-your-socks/</guid>
		<description><![CDATA[People who don&#8217;t know a thing about Objective-C are always asking me why I&#8217;d want to learn a special language &#8220;just for Mac programming&#8221; or why I like it more than most other programming languages in general. I think this is a great code snippet that made me stop and say you could NOT do [...]]]></description>
			<content:encoded><![CDATA[<p>People who don&#8217;t know a thing about Objective-C are always asking me why I&#8217;d want to learn a special language &#8220;just for Mac programming&#8221; or why I like it more than most other programming languages in general.  I think this is a great code snippet that made me stop and say you could NOT do this so elegantly in C++ or many other languages.</p>

<pre>
NSArray *participants = [archive valueForKey:@"participants"];
NSEnumerator *en = [participants objectEnumerator];
User *user = nil;

while (user = [en nextObject]) {                    
    // do stuff with the user
}
</pre>

<p>So what&#8217;s fascinating about this simple block of code? Let me explain.  Lets say <code>archive</code> is a regular <code>NSDictionary</code> object, and <code>participants</code> is an OPTIONAL key/value in this dictionary which could contain an array of <code>Users</code>.  How does that affect the rest of our code?  Well that means the <code>NSArray</code> <code>participants</code> will be nil.  So what happens when you call <code>objectEnumerator</code> on a nil object? An error? Nope! calling a message (any message) on an nil object returns nil.  So that means en, our enumerator, becomes nil.  Now when we get to our while loop calling <code>nextObject</code> on en will also return nil.  Thus <strong>the loop terminates gracefully</strong> without any error checking code, an no sig-faults from calling our &#8220;nil&#8221; objects.  I&#8217;ve just written code that not only performs it&#8217;s task but gracefully degrades even under optional conditions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otierney.net/articles/2007/01/20/objective-c-rocks-your-socks/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
