<?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>iOS Developer Tips Blog &#187; Objective-C</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/objective-c/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +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>Objective-C Comment Styles</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/objective-c-comment-styles.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/objective-c-comment-styles.html#comments</comments>
		<pubDate>Mon, 26 Sep 2011 08:12:40 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10069</guid>
		<description><![CDATA[A few weeks ago I wrote about Objective-C Indentation Styles. A reader (thanks Joe) commented and suggested a similar post that covered comment formats. That blog post follows&#8230; Interface File Comments When I create an interface file, here is the basic layout of my comments: /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * AboutViewController.h * * Created by John on 6/10/11. [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I wrote about <a href="http://iphonedevelopertips.com/objective-c/objective-c-indentation-styles.html" target="_blank">Objective-C Indentation Styles</a>. A reader (thanks Joe) commented and suggested a similar post that covered comment formats. That blog post follows&#8230;</p>
<h5>Interface File Comments</h5>
<p>When I create an interface file, here is the basic layout of my comments:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  AboutViewController.h
*
*  Created by John on 6/10/11.
*  Copyright 2011 iOSDeveloperTips.com. All rights reserved.
*
*  Long comments describing this object would go here...
*  ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
&nbsp;
<span style="color: #a61390;">@interface</span> AboutViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  UIButton          <span style="color: #002200;">*</span>backButton;  <span style="color: #11740a; font-style: italic;">// Comment here</span>
  <span style="color: #400080;">NSMutableArray</span>    <span style="color: #002200;">*</span>queueArray;  <span style="color: #11740a; font-style: italic;">// Comment here</span>
  <span style="color: #400080;">NSMutableArray</span>    <span style="color: #002200;">*</span>bucketArray;  <span style="color: #11740a; font-style: italic;">// Comment here</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">//-------------------------------------------------------</span>
  <span style="color: #11740a; font-style: italic;">// To describe something in more detail, it would go here</span>
  <span style="color: #11740a; font-style: italic;">// searchResultsCount varaible will hold ....</span>
  <span style="color: #11740a; font-style: italic;">// questionsResultsCount is calculated by ...</span>
  <span style="color: #11740a; font-style: italic;">//-------------------------------------------------------</span>
  <span style="color: #a61390;">int</span>               searchResultsCount;     <span style="color: #11740a; font-style: italic;">// Comment here</span>
  <span style="color: #a61390;">int</span>		    questionsResultsCount;  <span style="color: #11740a; font-style: italic;">// Comment here</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><span id="more-10069"></span></p>
<p>I&#8217;m not sure when it happened, somewhere on the way I moved away from the traditional comment block shown below, I don&#8217;t use this for the header or inside the primary code sections. I must of tired of the look and made a small tweak to us ~ and &#8211; characters.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/**********************************************************
* To describe something in more detail, it would go here
* searchResultsCount varaible will hold ....
*  questionsResultsCount is calculated by ...
**********************************************************/</span></pre></div></div>

<h5>Implementation File Comments</h5>
<p>At the top of the implementation file, my header looks the same as the interface file. If there is a private interface (see the post <a href="http://iphonedevelopertips.com/objective-c/private-methods.html" target="_blank">Private Methods</a> for more information), I follow the same layout:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  AboutViewController.m
*
*  Created by John on 6/10/11.
*  Copyright 2011 iOSDeveloperTips.com. All rights reserved.
*
*  Long comments describing this class would go here...
*  ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;AboutViewController.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
<span style="color: #a61390;">@interface</span> AboutViewController<span style="color: #002200;">&#40;</span>private<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> processSearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">string</span>;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>For methods in the class, my comment format looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Manage button precess events
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// For one line comment</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> backButton<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">//-------------------------------------------------------</span>
  <span style="color: #11740a; font-style: italic;">// If more than one line of comments is needed, </span>
  <span style="color: #11740a; font-style: italic;">// I use this format...</span>
  <span style="color: #11740a; font-style: italic;">//-------------------------------------------------------</span>
  ...
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Here is a template of what my code looks like with <a href="http://iphonedevelopertips.com/xcode/xcode-4-pragma-mark-shortcut.html" target="_blank">#pragma marks</a> included:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  AboutViewController.m
*
*  Created by John on 6/10/11.
*  Copyright 2011 iOSDeveloperTips.com. All rights reserved.
*
*  Long comments describing this object would go here...
*  ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;AboutViewController.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
<span style="color: #a61390;">@interface</span> AboutViewController<span style="color: #002200;">&#40;</span>private<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> processSearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">string</span>;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> AboutViewController
&nbsp;
<span style="color: #6e371a;">#pragma mark - Private Methods</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* This method....
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> processSearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">string</span>
<span style="color: #002200;">&#123;</span>
  ...  
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark - Initialization</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Initialization of ...
* More comments here...
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark - Event Management</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Manage button precess events
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// For one line comment</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> backButton<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">//-------------------------------------------------------</span>
  <span style="color: #11740a; font-style: italic;">// If more than one line of comments is needed, </span>
  <span style="color: #11740a; font-style: italic;">// I use this format...</span>
  <span style="color: #11740a; font-style: italic;">//-------------------------------------------------------</span>
  ...
&nbsp;
<span style="color: #002200;">&#125;</span>  
&nbsp;
<span style="color: #6e371a;">#pragma mark - Cleanup</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Cleanup code
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  ...
&nbsp;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The beauty of using #pragma mark is that within Xcode you can get a nice breakdown of the organization of your code:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/pragma.gif" alt="" title="pragma" width="287" height="257" class="alignnone size-full wp-image-10093" /></p>
<p>You can view this dialog from within Xcode by clicking on the class status bar in the boxed area shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/pragma2.gif" alt="" title="pragma2" width="595" height="232" class="alignnone size-full wp-image-10094" /></p>
<h5>Posting a Comment with Code</h5>
<p>Please feel free to post code examples of your comment style. If you would like have your code color highlighted as shown above, using the following format:</p>
<p>&lt;pre lang=&#8221;objc&#8221;&gt;<br />
  your code here<br />
&lt;/pre&gt;</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/objective-c-comment-styles.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Objective-C Indentation Styles</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/objective-c-indentation-styles.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/objective-c-indentation-styles.html#comments</comments>
		<pubDate>Tue, 06 Sep 2011 07:33:18 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9757</guid>
		<description><![CDATA[Discussions with developers about their preferred indentation style often seems to stir the same passion as one&#8217;s preference for beer, wine or cocktails. This post isn&#8217;t as much a tip or trick, as it is an opportunity for developers to share their variations and preferences when it comes to indenting code. Let me give you [...]]]></description>
			<content:encoded><![CDATA[<p>Discussions with developers about their preferred indentation style often seems to stir the same passion as one&#8217;s preference for beer, wine or cocktails.</p>
<p>This post isn&#8217;t as much a tip or trick, as it is an opportunity for developers to share their variations and preferences when it comes to indenting code. Let me give you a quick run-down of how my Objective-C code typically looks. As you&#8217;ll see, if nothing else, I am consistent&#8230;<br />
<span id="more-9757"></span></p>
<h5>Functions/Methods</h5>
<p>I like my braces on a line by themselves:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> loginAttemptComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>results
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This is consistent across all of my code, for example, here is how an interface definitions may look:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> LoginViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As far as parameters, I like to keep them on the same line:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Versus this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span>argc, argv<span style="color: #002200;">&#41;</span>
         <span style="color: #a61390;">int</span>   argc;
         <span style="color: #a61390;">char</span>  <span style="color: #002200;">*</span>argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>If the list gets lengthy, I will wrap and indent:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchBar<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UISearchBar <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>searchBar selectedScope<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>selectedScope didChange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>didChange
                  top<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>top                  
                  color<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIColor<span style="color: #002200;">&#41;</span>color
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Calling Functions/Methods</h5>
<p>Again, I like to keep everything on one line when possible, this includes calling a function or method. I am generally okay if my lines extend past 80 characters given the expansive screen resolutions available on most machines:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>someObj<span style="color: #002200;">:</span> searchPrefs<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UISearchPrefs <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>searchPrefs selectedScope<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>selectedScope didChange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>didChange<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>However, I do have my limits on what is reasonable to squeeze onto one line;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self showQuestionAndAnswer<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SharedData sharedModel<span style="color: #002200;">&#93;</span> attributes<span style="color: #002200;">&#93;</span> answerID<span style="color: #002200;">&#93;</span>
                               questionID<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SharedData sharedModel<span style="color: #002200;">&#93;</span> attributes<span style="color: #002200;">&#93;</span> questionID<span style="color: #002200;">&#93;</span>
                               filterID<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SharedData sharedModel<span style="color: #002200;">&#93;</span> attributes<span style="color: #002200;">&#93;</span> filerID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>If else/if, while, for</h5>
<p>Here is how I like to setup my if/else statements:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>returnData<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">else</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Also, I don&#8217;t include braces if the code does not require them:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>searchObj noSearchData<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  searchFlag <span style="color: #002200;">=</span> RESET;
<span style="color: #a61390;">else</span>
  searchFlag <span style="color: #002200;">=</span> UPDATE;</pre></div></div>

<p>My if/else statements look as follows, where the <em>else if</em> are on the same line:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>returnData<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>httpResponse statusCode<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> HTTP_OK<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As with the above, when I write while and for loops, braces stand alone:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>obj currentCharacter<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #bf1d1a;">'?'</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dict <span style="color: #a61390;">in</span> dataList<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Tab Stops</h5>
<p>I prefer to indent two spaces, and always opt for space characters over tabs. For those who use tabs, is there a primary benefit of tabs that makes you choose one over the other?</p>
<h5>Posting a Comment with Code</h5>
<p>Please feel free to post code examples of your style. If you would like to post a comment and have your code color highlighted as shown above, using the following format:</p>
<p>&lt;pre lang=&#8221;objc&#8221;&gt;<br />
  your code here<br />
&lt;/pre&gt;</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/objective-c-indentation-styles.html/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Introduction to Blocks in Objective-C – Part 2</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/introduction-to-blocks-in-objective-c-%e2%80%93-part-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/introduction-to-blocks-in-objective-c-%e2%80%93-part-2.html#comments</comments>
		<pubDate>Mon, 29 Aug 2011 08:45:19 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9605</guid>
		<description><![CDATA[In the first post on blocks, Introduction to Blocks in Objective-C &#8211; Part 1, I covered the basics for creating block variables, working with __block modifier and using typedef to define blocks. In this post I will show how you can pass a block as a parameter, use enumeration with blocks, as well as take [...]]]></description>
			<content:encoded><![CDATA[<p>In the first post on blocks, <a href="http://iphonedevelopertips.com/objective-c/introduction-to-blocks-in-objective-c-part-1.html" target="_blank">Introduction to Blocks in Objective-C &#8211; Part 1</a>, I covered the basics for creating block variables, working with __block modifier and using typedef to define blocks. In this post I will show how you can pass a block as a parameter, use enumeration with blocks, as well as take a quick look at blocks and recursion.</p>
<h5>Passing Blocks as Parameters</h5>
<p>Within a number of iOS frameworks, methods are provided that accept a block as a parameter. As it relates to working with blocks, this is one area where things start to get interesting and you get a perspective on a few unique features offered through blocks.<br />
<span id="more-9605"></span></p>
<p>For example, in the code below, I create an array of string objects, nothing new there. However, in iOS 4+<strong>NSArray</strong> now includes a method to create a set (<strong>NSSet</strong>) object from the array by passing in a block into the <strong>indexesOfObjectsPassingTest</strong> method. The block that I&#8217;ve written checks the length of each object in the array (each is object is string in this example), and return YES, only for those objects where the string is greater than 5 in length.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create an array</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;12345&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;12345678&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;abcd&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;123abc&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create a set (an unordered collection of objects) with the indexes of the objects</span>
<span style="color: #11740a; font-style: italic;">// in the array where the length of the object in the array is &gt; 5.</span>
<span style="color: #11740a; font-style: italic;">// Notice the parameter is a block.</span>
<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>set <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>array indexesOfObjectsPassingTest<span style="color: #002200;">:</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> obj, NSUInteger idx, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span>  
<span style="color: #002200;">&#123;</span>                   
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>obj length<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #a61390;">else</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Now that we have a set that tells us which objects are of a specified length (or greater), let&#8217;s call a method in the <strong>NSSet</strong> object that will enumerate the values in the set and print the values of the relevant array entries &#8211; the method <strong>enumerateIndexesUsingBlock</strong> in <strong>NSSet</strong> will also accept a block as a parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Enumerate the set, printing the     </span>
<span style="color: #002200;">&#91;</span>set enumerateIndexesUsingBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span>NSUInteger idx, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Array entry %d contains: %@&quot;</span>, idx, <span style="color: #002200;">&#91;</span>array objectAtIndex<span style="color: #002200;">:</span>idx<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The output from the above is:</p>
<p><strong>Array entry 1 contains: 12345678<br />
Array entry 3 contains: 123abc<br />
</strong></p>
<h5>Using Stop to End Processing</h5>
<p>Let&#8217;s take a closer look at the definition of <strong>indexOfObjectPassingTest</strong> method in the <strong>NSSArray</strong> object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>indexOfObjectPassingTest<span style="color: #002200;">:</span>
     <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> obj, NSUInteger idx, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>predicate</pre></div></div>

<p>In the first example shown above, the only parameter in the block referenced was <strong>obj</strong>, where the length was compared to see if it was > 5. When you need to stop processing a block based on some condition check, you can use the <strong>stop</strong> parameter.</p>
<p>Let&#8217;s say we have an array of dictionary objects that contain an account number and a boolean flag that indicates if the account is active:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayOfDictionaryObjects <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1234&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;2345&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;3456&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;4567&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If we had a need to process the array and find the first account which was flagged as active, we could once again use the <strong>indexOfObjectPassingTest</strong> method of the <strong>NSArray</strong> object, passing in a block, and looking at the value of the <strong>isActive</strong> key to find the first active account:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSUInteger activeAccount <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arrayOfDictionaryObjects indexOfObjectPassingTest<span style="color: #002200;">:</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> obj, NSUInteger idx, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span>                                                                        
<span style="color: #002200;">&#123;</span>            
  <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>num <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>obj valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>num boolValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Stop processing the block</span>
    <span style="color: #002200;">*</span>stop <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Notice how once the condition is met, we set the stop value to YES, this will stop any further processing of the block (after the &#8216;return YES&#8217; statement or course).</p>
<h5>Blocks and Enumeration</h5>
<p>Enumeration is a common operation on objects, and there are numerous variations of block operations to help. In the code below I create a dictionary of timezone abbreviations and their names:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create dictionary of timezone abbreviations and names</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> abbreviationDictionary<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Abbreviations: %@&quot;</span>, dict<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from the above looks like this:</p>
<p><strong>Abbreviations: {<br />
    ADT = &#8220;America/Halifax&#8221;;<br />
    AKDT = &#8220;America/Juneau&#8221;;<br />
    AKST = &#8220;America/Juneau&#8221;;<br />
    ART = &#8220;America/Argentina/Buenos_Aires&#8221;;<br />
    AST = &#8220;America/Halifax&#8221;;<br />
    &#8230;<br />
    }<br />
</strong>  </p>
<p>We can now enumerate over the dictionary keys and objects, using a block, to print out the abbreviations that have the prefix &#8220;America&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>dict enumerateKeysAndObjectsUsingBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> key, <span style="color: #a61390;">id</span> obj, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>obj hasPrefix<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;America&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;America Timezone: %@&quot;</span>, key<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// To view all key/value pairs</span>
<span style="color: #11740a; font-style: italic;">//  NSLog(@&quot;Key: %@ Value: %@&quot;,key, obj);</span>
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The American timezones are shown as:</p>
<p><strong>America Timezone: EDT<br />
America Timezone: AST<br />
America Timezone: PET<br />
America Timezone: CLST<br />
America Timezone: PST<br />
&#8230;<br />
</strong></p>
<p>Let&#8217;s look at one more example using the same dictionary that was defined above, where one value in the dictionary is the account number and one indicates if the account is active:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayOfDictionaryObjects <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1234&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;2345&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;3456&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;4567&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>To print the active accounts from the array of dictionaries, (notice for this example both the 1st and 3rd entries are set as active), we can enumerate the array using a block and check the value of the &#8220;isActive&#8221; key in the dictionary:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>arrayOfDictionaryObjects enumerateObjectsUsingBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> object, NSUInteger index, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Print the active accounts:</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>object valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Active account: %@&quot;</span>, <span style="color: #002200;">&#91;</span>object valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The output is shown as:</p>
<p><strong>Active account: 1234<br />
Active account: 3456<br />
</strong></p>
<h5>Recursion and Blocks</h5>
<p>Writing a recursive block is pretty straight-forward. Let&#8217;s look at a few examples.</p>
<p>Calculating factorials is a common example when speaking recursion. 4 factorial is expressed as 4!, which evaluates to the sum of 4 * 3 * 2 * 1. A block variable below is defined to calculate factorials:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">__block <span style="color: #a61390;">int</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>factorial<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>;
&nbsp;
factorial <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> x<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
&nbsp;
  <span style="color: #a61390;">return</span> x <span style="color: #002200;">*</span> factorial<span style="color: #002200;">&#40;</span>x <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>;  
&nbsp;
<span style="color: #a61390;">int</span> y <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d factorial is: %d&quot;</span>, y, factorial<span style="color: #002200;">&#40;</span>y<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>There is really nothing special about recursion and blocks, with the one important exception, the __block modifier is required in the definition of the block.</p>
<p>A discussion on recursion wouldn&#8217;t be complete without visiting the Fibonacci sequence. If it&#8217;s been a while, the Fibonacci sequence goes as follows:</p>
<p>0 1 1 2 3 5 8 13 21 34 21&#8230;</p>
<p>where the next number in the series is the sum of the previous two values: 0 + 1 = 1; 1 + 1 = 2; 1 + 2 = 3 etc. </p>
<p>The sequence is written as:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/08/fib.png" alt="" title="fib" width="156" height="18" class="alignnone size-full wp-image-9615" /></p>
<p>A partial series follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/08/fib2.png" alt="" title="fib2" width="521" height="63" class="alignnone size-full wp-image-9628" /></p>
<p>Below is block that will generate a value in the Fibonacci sequence:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">__block <span style="color: #a61390;">int</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>fibonacci<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>;
&nbsp;
fibonacci <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> y<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>y <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>y <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">else</span>
    <span style="color: #a61390;">return</span> fibonacci<span style="color: #002200;">&#40;</span>y <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> fibonacci<span style="color: #002200;">&#40;</span>y <span style="color: #002200;">-</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>To get the value for F7:</p>
<p>int x = 7;<br />
NSLog(@&#8221;Fibonacci: %d&#8221;, fibonacci(x));
</pre>
<p>The output is: <strong>Fibonacci: 13</strong></p>
<h5>What's Next...</h5>
<p>Honestly, I'm not quite sure where this will go next - callbacks and view animations/transitions might be a good direction...</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/introduction-to-blocks-in-objective-c-%e2%80%93-part-2.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introduction to Blocks in Objective-C &#8211; Part 1</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/introduction-to-blocks-in-objective-c-part-1.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/introduction-to-blocks-in-objective-c-part-1.html#comments</comments>
		<pubDate>Mon, 22 Aug 2011 06:09:05 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9478</guid>
		<description><![CDATA[Beginning with iOS 4.0, Apple introduced blocks, which look and operate much like C functions. However, blocks offer many interesting capabilities beyond functions as you know and love them today. A block is really nothing more than a chunk of code. What makes them unique is that a block can be executed inline as well [...]]]></description>
			<content:encoded><![CDATA[<p>Beginning with iOS 4.0, Apple introduced blocks, which look and operate much like C functions. However, blocks offer many interesting capabilities beyond functions as you know and love them today. </p>
<p>A block is really nothing more than a chunk of code. What makes them unique is that a block can be executed inline as well as passed as an argument into a method/function. Blocks can also be assigned to a variable and called as you would a C function &#8211; this, along with a few other topics will be covered in this post.<br />
<span id="more-9478"></span></p>
<h5>Block Variable</h5>
<p>To define a block variable, the ^ operator is used. Let&#8217;s look at a simple block that will return YES/NO based on whether the passed in integer is an even or odd value:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Defining a block variable</span>
<span style="color: #a61390;">BOOL</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>isInputEven<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> input<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>input <span style="color: #002200;">%</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #a61390;">else</span> 
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>The diagram below describes each section of the block:</p>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/08/block1.png"><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/08/block1.png" alt="" title="block1" width="564" height="250"/></a></p>
<p>To call the block look similar to a C function call:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Call similar to a C function call</span>
<span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">101</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d %@ number&quot;</span>, x, isInputEven<span style="color: #002200;">&#40;</span>x<span style="color: #002200;">&#41;</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;is an even&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;is not an even&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output for the log statement is:</p>
<p><strong>-101 is not an even number</strong></p>
<h5>Blocks and Variable Scope</h5>
<p>Notice in the block below that the code in the body references the variable &#8216;price&#8217; which is defined outside the block.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">float</span> price <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.99</span>;
&nbsp;
<span style="color: #a61390;">float</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>finalPrice<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> quantity<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Notice local variable price is </span>
  <span style="color: #11740a; font-style: italic;">// accessible in the block</span>
  <span style="color: #a61390;">return</span> quantity <span style="color: #002200;">*</span> price;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #a61390;">int</span> orderQuantity <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ordering %d units, final price is: $%2.2f&quot;</span>, orderQuantity, finalPrice<span style="color: #002200;">&#40;</span>orderQuantity<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output for the log statement is:</p>
<p><strong>Ordering 10 units, final price is: $19.90</strong></p>
<p>Let&#8217;s change the price and run the block again:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">price <span style="color: #002200;">=</span> .99;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ordering %d units, final price is: $%2.2f&quot;</span>, orderQuantity, finalPrice<span style="color: #002200;">&#40;</span>orderQuantity<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Seems changing the price should update the return value from the block, however, during the block definition the price variable is set to a const, so the output remains the same as before:</p>
<p>The output for the block, with the price variable updated is:</p>
<p><strong>Ordering 10 units, final price is: $19.90</strong></p>
<h5>Using __block Storage Modifier</h5>
<p>To allow a variable defined outside a block to be mutable, apply the __block storage type modifier:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Use the __block storage modifier to allow changes to 'price'</span>
__block <span style="color: #a61390;">float</span> price <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.99</span>;
&nbsp;
<span style="color: #a61390;">float</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>finalPrice<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> quantity<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> quantity <span style="color: #002200;">*</span> price;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #a61390;">int</span> orderQuantity <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;
price <span style="color: #002200;">=</span> .99;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;With block storage modifier - Ordering %d units, final price is: $%2.2f&quot;</span>, orderQuantity, finalPrice<span style="color: #002200;">&#40;</span>orderQuantity<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output for the block, with the price variable updated is:</p>
<p><strong>With block storage modifier &#8211; Ordering 10 units, final price is: $9.90</strong></p>
<h5>Typedef and Blocks</h5>
<p>Before we get to typedef, let&#8217;s look at two different ways to define a block variable. In the examples above, the block, parameters and body are defined in one fell swoop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">BOOL</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>isInputEven<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> input<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>input <span style="color: #002200;">%</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #a61390;">else</span> 
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>This is equivalent to creating a block definition:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">BOOL</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>isInputEven<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>and at some point later, defining the body:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">isInputEven <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> input<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>input <span style="color: #002200;">%</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #a61390;">else</span> 
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>This above is important, as it may make sense at some point to have the same signature for two distinct blocks. This is where the typdef comes in &#8211; a best practice is to use typedef if the same block signature is used for two or more unique operations:</p>
<p>For example, below a typedef is defined for a block that accepts two objects and returns an <strong>NSComparisonResult</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> NSComparisonResult<span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>CompareObjects<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><strong>NSComparisonResult</strong> is an enum that can be used to indicate the ordering of items (ascending, descending, same).</p>
<p>With the typedef in place you could now write two blocks, both accepting objects, however each performing a different comparison:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CompareObjects compareAccountBalance <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> objectA, <span style="color: #a61390;">id</span> objectB<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Do comparisons here...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// return NSComparisonResult value (NSOrderedAscending, NSOrderedSame, NSOrderedDescending)</span>
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>Here is another block with the same signature, however, there you could change up the logic for comparing the objects:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CompareObjects compareAccountActivity <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> objectA, <span style="color: #a61390;">id</span> objectB<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Do comparisons here...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// return NSComparisonResult value (NSOrderedAscending, NSOrderedSame, NSOrderedDescending)</span>
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<h5>Summary</h5>
<p>This post covered the basics of blocks, including block variables, the __block storage modifier and using typedef to define a block.</p>
<p>In an upcoming post we&#8217;ll look at a few more interesting aspects of blocks, such as passing a block as a parameter to a method, enumeration with blocks, as well as using recursion in a block.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/introduction-to-blocks-in-objective-c-part-1.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using the @class Directive</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/using-the-class-directive.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/using-the-class-directive.html#comments</comments>
		<pubDate>Mon, 03 Jan 2011 08:09:34 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=7274</guid>
		<description><![CDATA[The @class directive often seems to be a point of confusion. Let me try and provide a little insight. The @class directive sets up a forward reference to another class. For example, in the code below the reference to @class HomeBrewRecipes informs the compiler that HomeBrewRecipes is indeed a class, so when the compiler gets [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>@class</strong> directive often seems to be a point of confusion. Let me try and provide a little insight.</p>
<p>The <strong>@class</strong> directive sets up a forward reference to another class. For example, in the code below the reference to <strong>@class HomeBrewRecipes</strong> informs the compiler that <strong>HomeBrewRecipes</strong> is indeed a class, so when the compiler gets to line 10 no additional information is needed, it assumes all is well and forges ahead.<br />
<span id="more-7274"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  HomeBrewBook.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #a61390;">@class</span> HomeBrewRecipes;
&nbsp;
<span style="color: #a61390;">@interface</span> HomeBrewBook <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  
<span style="color: #002200;">&#123;</span>  
  <span style="color: #a61390;">int</span>  pageCount;
  HomeBrewRecipes  <span style="color: #002200;">*</span>recipes;  
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>When <strong>HomeBrewRecipes</strong> class is actually referenced, for example an instance of the class is created, the class interface will need to be imported &#8211; put another way, the class that creates an instance of <strong>HomeBrewRecipes</strong> will need to include <strong>HomeBrewRecipes.h</strong>, as is shown in the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  HomeBrewBook.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;HomeBrewRecipes.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> HomeBrewBook
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>someMethod
<span style="color: #002200;">&#123;</span> 
  recipes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>HomeBrewRecipes alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>What Not Just Always Import?</h5>
<p>In many cases you can import the interface file and achieve the same end result.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #6e371a;">#import &quot;HomeBrewRecipes.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> HomeBrewBook <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  
<span style="color: #002200;">&#123;</span>  
  <span style="color: #a61390;">int</span>  pageCount;
  HomeBrewRecipes  <span style="color: #002200;">*</span>recipes;  
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So why bother with @class? The primary reason is to avoid circular references, and the problems of tracking down errors generated by the same. For example, if Class X imports Class Y, and Class Y imports class X, there&#8217;s a good chance you may run into a compile time error. </p>
<p>There are exceptions where you will need to import the relevant .h file(s) versus using @class &#8211; if in the example above we needed to reference a method in <strong>HomeBrewBook</strong> within <strong>HomeBrewBook.h</strong>, the compiler would need the specifics on the method call (its parameters, etc).</p>
<h5>Best Practice</h5>
<p>To keep things consistent in your code, I would recommend you use the @class directive to make a forward reference to classes within interface files (like the first code example) and only import the .h file if needed.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/using-the-class-directive.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>The Basics of Protocols and Delegates</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/the-basics-of-protocols-and-delegates.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/the-basics-of-protocols-and-delegates.html#comments</comments>
		<pubDate>Fri, 16 Apr 2010 12:00:32 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6113</guid>
		<description><![CDATA[Apple offers a good overview of working with protocols in their Objective-C Programming Reference. However, sometimes a simple working example can go a long ways&#8230; Introduction Protocols can be helpful in a number of scenarios, a common usage is to define methods that are to be implemented by other classes. A familiar example is when [...]]]></description>
			<content:encoded><![CDATA[<p>Apple offers a good overview of working with protocols in their <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html#//apple%5Fref/doc/uid/TP30001163-CH15-SW1">Objective-C Programming Reference</a>. However, sometimes a simple working example can go a long ways&#8230;</p>
<h5>Introduction</h5>
<p>Protocols can be helpful in a number of scenarios, a common usage is to define methods that are to be implemented by other classes. A familiar example is when using a tableview, your class implements the <strong>cellForRowAtIndexPath</strong> method  which asks for cell content to insert into a table &#8211; the <strong>cellForRowAtIndexPath</strong> method is defined within the <strong>UITableViewDataSource</strong> protocol.</p>
<p>Let&#8217;s walk through a very simple example of defining and adopting a protocol.<br />
<span id="more-6113"></span></p>
<h5>Protocol Definition</h5>
<p>Here is an example of a protocol which includes one method, notice the instance variable <strong>delegate</strong> is of type <strong>id</strong>, as it will be unknown at compile time the type of class that will adopt this protocol.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@protocol</span> ProcessDataDelegate &lt;NSObject&gt;
@required
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> processSuccessful<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>success;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ClassWithProtocol <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
 <span style="color: #a61390;">id</span> &lt;ProcessDataDelegate&gt; delegate;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>retain<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> delegate;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startSomeProcess;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Protocol Implementation</h5>
<p>Inside the implementation section for the interface defined above we need to do two things at a minimum &#8211; first synthesize the delegate instance variable and second, call the method defined in the protocol as needed (more on that in a moment).</p>
<p>Let&#8217;s look at a bare bones implementation of the <strong>ClassWithProtocol.m</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ClassWithProtocol.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ClassWithProtocol
&nbsp;
<span style="color: #a61390;">@synthesize</span> delegate;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>processComplete
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self delegate<span style="color: #002200;">&#93;</span> processSuccessful<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startSomeProcess
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimer</span> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">5.0</span> target<span style="color: #002200;">:</span>self 
    selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>processComplete<span style="color: #002200;">&#41;</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Understand this is a rather contrived example &#8211; the intention is to show how/where one might use a protocol. For the sake of discussion assume you have a class that is processing (or downloading) some type of data. Further, assume this class is called from another class to begin the processing. Chances are, at some point the caller will want to be notified that the class processing the data is done, this is where the protocol comes in.</p>
<p>In the calling class, the method defined in the protocol, <strong>processSuccessful</strong>, will be implemented and will be called from the object doing the processing, once it is complete. </p>
<p>For this example, inside the class where the protocol is defined, I have one method, <strong>startSomeProcess</strong>, which simply starts a timer and calls <strong>processComplete</strong> after 5 seconds. Inside <strong>processComplete</strong> the calling object will be notified through its delegate that the process is done.</p>
<h5>Adopting the Protocol</h5>
<p>To keep the example short, I am using the applicaton delegate as the class that adopts the protocol. Here is how the app delegate looks:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &quot;ClassWithProtocol.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TestAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;UIApplicationDelegate, ProcessDataDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UIWindow <span style="color: #002200;">*</span>window;
  ClassWithProtocol <span style="color: #002200;">*</span>protocolTest;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>A few things to note &#8211; <strong>ProcessDataDelegate</strong> is defined as part of the interface, which signifies that this class will adhere to the protocol. Looking back to the code for defining the protocol, notice that I added <strong>@required</strong> to the definition, which means that any class that adopts the protocol must implement the <strong>processComplete</strong> method (you will receive a compile warning if you don&#8217;t).</p>
<p>Here is the implementation of the app delegate and the required method for the protocol:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;TestAppDelegate.h&quot;</span>
<span style="color: #6e371a;">#import &quot;ClassWithProtocol.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> TestAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window;
&nbsp;
 UITableViewDelegate
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>processSuccessful<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>success;
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Process completed&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span>   
  <span style="color: #11740a; font-style: italic;">// Create and initialize the window</span>
  window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  protocolTest <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ClassWithProtocol alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>protocolTest setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>protocolTest startSomeProcess<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>window release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>How it all works</h5>
<p>Things goes as follows: the app delegate will create a new instance of the <strong>ClassWithProtocol</strong> object. It sets itself as the delegate and then calls the <strong>startSomeProcess</strong> method. At some point in the future, when the <strong>protocolTest</strong> object has completed its work &#8211; after the 5 second timer has fired &#8211; it will call the <strong>processSuccessful</strong> method in the app delegate to let it know it is done processing.</p>
<h5>Download Xcode Project</h5>
<p>You can find a complete (albeit trivial) working example here: <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/protocolExample.zip">Protocol Xcode Project</a></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/the-basics-of-protocols-and-delegates.html/feed</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>Fast Enumeration on the iPhone</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/fast-enumeration-on-the-iphone.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/fast-enumeration-on-the-iphone.html#comments</comments>
		<pubDate>Fri, 02 Oct 2009 12:10:51 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4052</guid>
		<description><![CDATA[With the introduction of Objective-C 2.0, you can now iterate through collections (arrays, etc) with ease using a language feature known as Fast Enumeration. This enumeration is both faster than using NSEnumerator and is also much more concise as it relates to the syntax, resulting in code that is easier on the eyes. The basic [...]]]></description>
			<content:encoded><![CDATA[<p>With the introduction of Objective-C 2.0, you can now iterate through collections (arrays, etc) with ease using a language feature known as Fast Enumeration. </p>
<p>This enumeration is both faster than using <strong>NSEnumerator</strong> and is also much more concise as it relates to the syntax, resulting in code that is easier on the eyes.<br />
<span id="more-4052"></span></p>
<p>The basic syntax looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>classType variable <span style="color: #a61390;">in</span> expression <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span> 
  statements 
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Fast Enumeration Forward with Array</h5>
<p>As an example, to iterate through an array, beginning to end, it&#8217;s as simple as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span> 
   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hefeweizen&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Pilsner&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stout&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>element <span style="color: #a61390;">in</span> array<span style="color: #002200;">&#41;</span> 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer: %@&quot;</span>, element<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>Fast Enumeration Backwards with Array</h5>
<p>Fast Enumeration supports using <strong>NSEnumerator</strong> objects, so for example, if you want to output the same array shown above in reverse order, call the the <strong>reverseObjectEnumerator</strong> method on the array, which will return a <strong>NSEnumerator</strong> and use that within the Fast Enumeration loop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>element <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>array reverseObjectEnumerator<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer: %@&quot;</span>, element<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>And if you are concerned about performance of the above, you can sleep well knowing that the call to <strong>[array reverseObjectEnumerator]</strong> is evaluated only the first time through the loop.</p>
<h5>Fast Enumeration with Dictionary</h5>
<p>Enumerating through a dictionary is equally as simple as looping through an array &#8211; access the key as an <strong>NSString</strong> object and use that value in the <strong>valueForKey</strong> method as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stout&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;dark&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hefeweizen&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;wheat&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hoppy&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dictionary<span style="color: #002200;">&#41;</span> 
   NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ beer example: %@&quot;</span>, key, <span style="color: #002200;">&#91;</span>dictionary valueForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/beers1.png" /></p>
<h5>Documentation</h5>
<p>You can read more about <a  target="_blank"  href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocFastEnumeration.html">Fast Enumeration here</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/fast-enumeration-on-the-iphone.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Specifying Simulator Only Code</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/simulator-specific-code.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/simulator-specific-code.html#comments</comments>
		<pubDate>Wed, 15 Apr 2009 03:28:47 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2088</guid>
		<description><![CDATA[There are times when you need to write code that is only applicable when working with the simulator. As an example, I was recently working with an application that required the user to take a picture with their iPhone. Once the picture was taken the application was to show the image to the user, at [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you need to write code that is only applicable when working with the simulator. </p>
<p>As an example, I was recently working with an application that required the user to take a picture with their iPhone. Once the picture was taken the application was to show the image to the user, at which point they would measure the size of an object in the image using various controls (movable lines) and a reference object in the image. Problem is, the simulator does not have much for camera functionality (obviously, there is no lens) so any picture taken shows up as a black image.<br />
<span id="more-2088"></span></p>
<p>To get around this problem, I used the compiler directive below and inside the simulator section I would load an image and use the same throughout the application as if it was created from the camera.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#if TARGET_IPHONE_SIMULATOR</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Running on Simulator&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #6e371a;">#else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Running on Device&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>Working with a directive such as this is really handy as you can move between testing with the simulator and a real device without having to change any code.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/simulator-specific-code.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Of BOOL and YES</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/of-bool-and-yes.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/of-bool-and-yes.html#comments</comments>
		<pubDate>Wed, 31 Dec 2008 19:28:56 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422</guid>
		<description><![CDATA[It may not be immediately clear that the Objective C BOOL &#34;type&#34; is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a bool type). To clarify, [...]]]></description>
			<content:encoded><![CDATA[<p>It may not be immediately clear that the Objective C <code>BOOL</code> &quot;type&quot; is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a <code>bool</code> type). To clarify, in the system header file <code>objc.h</code> you can see the following:<br />
<span id="more-1422"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">signed</span> <span style="color: #a61390;">char</span>        <span style="color: #a61390;">BOOL</span>;</pre></div></div>

<p>From this it is clear that <code>BOOL</code> is actually a signed char type. This has some implications when it comes to using the <code>YES</code> and <code>NO</code> values that are typically assigned to variables of &quot;type&quot; <code>BOOL</code>. If we look a little further down <code>objc.h</code> you will see the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define YES             (BOOL)1</span>
<span style="color: #6e371a;">#define NO              (BOOL)0</span></pre></td></tr></table></div>

<p>So what is the problem with <code>BOOL</code> and <code>YES</code>? In a nutshell, the problem is that variables of type <code>BOOL</code> can contain values other than <code>YES</code> and <code>NO</code>. Comparing for <code>NO</code> is not an issue since <code>NO == 0 == NO</code>, but comparing for <code>YES</code> can be tricky, especially if you are relying on 3rd party code to play by the rules. Consider this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">BOOL</span> b <span style="color: #002200;">=</span> <span style="color: #2400d9;">37</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>b<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;b is YES!<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>b <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;b is not YES!<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The output from this would be as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">b is <span style="color: #a61390;">YES</span><span style="color: #002200;">!</span>
b is not <span style="color: #a61390;">YES</span><span style="color: #002200;">!</span></pre></td></tr></table></div>

<p>The problem arises because direct comparison with <code>YES</code> will fail when the value of a <code>BOOL</code> type is a non-zero value other than 1.</p>
<p>Note that ObjC (assuming the default C99 setting is enabled) also supports the <code>bool</code> data type, which is an intrinsic boolean type and can only be set to <code>true</code> or <code>false</code>. A safe way to convert between <code>BOOL</code> and <code>bool</code> is as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// from BOOL to bool</span>
bool b <span style="color: #002200;">=</span> myBOOL ? <span style="color: #a61390;">true</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">false</span>;
<span style="color: #11740a; font-style: italic;">// and back</span>
myBOOL <span style="color: #002200;">=</span> b ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;</pre></td></tr></table></div>

<p>The moral of the story is to avoid direct comparisons between <code>BOOL</code> types and the <code>YES</code> constant.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/of-bool-and-yes.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Class Variables</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/class-variables.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/class-variables.html#comments</comments>
		<pubDate>Wed, 10 Dec 2008 11:04:03 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1141</guid>
		<description><![CDATA[I previously wrote about the lack of support for private methods when working with Objective-C. As part of that post I presented a few work-arounds. Along the same lines, there are is no support for class variables in Objective-C. This post will explore this a little further and walk through a short example that shows [...]]]></description>
			<content:encoded><![CDATA[<p>I <a target="_blank" href="http://iphonedevelopertips.com/objective-c/private-methods.html">previously wrote</a> about the lack of support for private methods when working with Objective-C. As part of that post I presented a few work-arounds. Along the same lines, there are is no support for class variables in Objective-C. This post will explore this a little further and walk through a short example that shows how you can use static variables to arrive at a similar result to working with class variables.<br />
<span id="more-1141"></span></p>
<p>The reason for this discussion is that I recently had a need to track a few values that were tied to a specific class, not instances of the class. Given that using class variables was not an option, here&#8217;s what I did instead. First, I defined static variables for each value I needed to track. Next, I wrote methods that are private to the implementation to work with those values (think getter/setter). Caveat: there really is no such thing as a private method in Objective-C, however, you can arrive at a similar result as shown here: <a target="_blank"  href="http://iphonedevelopertips.com/objective-c/private-methods.html">private methods</a>.</p>
<p>The reason for choosing static variables is that the scope of these variables is limited to the class file where they are defined. In addition, static variables are not inherited by subclasses. </p>
<p>What follows is a short example where I use a static integer to track how many instances of a specific class are created. The interface for the class follows:</p>
<p><strong>TestClass Interface</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  TestClass.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TestClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>instanceCount;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Note that the instanceCount method is a class method. The implementation file for TestClass has a few interesting things to explore, have a look:</p>
<p><strong>TestClass Implementation</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  TestClass.m</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;TestClass.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #11740a; font-style: italic;">// Static variables </span>
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #11740a; font-style: italic;">// Count of objects allocated</span>
<span style="color: #a61390;">static</span> NSUInteger instanceCount;
&nbsp;
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #11740a; font-style: italic;">// Private methods defined</span>
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #a61390;">@interface</span> TestClass <span style="color: #002200;">&#40;</span>private<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>bumpInstanceCount;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #11740a; font-style: italic;">// Private methods implementation</span>
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #a61390;">@implementation</span> TestClass <span style="color: #002200;">&#40;</span>private<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>bumpInstanceCount
<span style="color: #002200;">&#123;</span>
  instanceCount <span style="color: #002200;">+=</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*-------------------------------------
* TestClass implementation
*-------------------------------------*/</span>
<span style="color: #a61390;">@implementation</span> TestClass
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>TestClass bumpInstanceCount<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>instanceCount;
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> instanceCount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>On line 9 notice the definition of the static variable. This variable is only accessible from within this implementation file. On lines 14-16 I define a category as a means to create methods that are private to this implementation, that is, not directly exposed to the outside world as are the contents of TestClass.h. Inside the private implementation section I have written a class method to increment the static variable.</p>
<p>Beginning on line 33 is the code for the TestClass implementation. Worthy of noting is line 38 where upon initializing each new object of type TestClass I call the class method &#8216;bumpInstanceCount&#8217; to update the count of TestClass objects created, that is, update our static (&#8220;class&#8221;) variable.</p>
<p>There is one more class method &#8216;instanceCount&#8217; where I return the current value of the static variable (lines 43 &#8211; 46). Notice that this method is part of the TestClass implementation rather than the category for private methods. This is the only means of accessing the static variables from outside the class.</p>
<p>What follows is the App Delegate code for exercising TestClass:</p>
<p><strong>AppDelegate Interface</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UntitledAppDelegate.h</span>
<span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> UntitledAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;UIApplicationDelegate&gt; 
<span style="color: #002200;">&#123;</span>
    UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>AppDelegate Implementation</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UntitledAppDelegate.m</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;UntitledAppDelegate.h&quot;</span>
<span style="color: #6e371a;">#import &quot;testClass.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UntitledAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span>    
  TestClass <span style="color: #002200;">*</span>tmp1, <span style="color: #002200;">*</span>tmp2;
&nbsp;
  tmp1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;instance count: %d&quot;</span>, <span style="color: #002200;">&#91;</span>TestClass instanceCount<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
  tmp2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;instance count: %d&quot;</span>, <span style="color: #002200;">&#91;</span>TestClass instanceCount<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>tmp1 release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>tmp2 release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>window release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>The output looks as follows:</p>
<p><img src="http://iphonedevelopertips.com/wp-content/uploads/2008/12/staticvar.png" alt="" /></p>
<p>You can extend this concept to other data types as well, for example, give it a go by experimenting with a C structure or array.</p>
<p>If you are interested to learn more about using categories, look over <a target="_blank" href="http://iphonedevelopertips.com/objective-c/categories.html">here</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/class-variables.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

