<?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>Mac Developer Tips &#187; Objective-C</title>
	<atom:link href="http://MacDeveloperTips.com/category/objective-c/feed" rel="self" type="application/rss+xml" />
	<link>http://MacDeveloperTips.com</link>
	<description>Tips, tools and code for iPhone and Mac developers.</description>
	<lastBuildDate>Thu, 12 Aug 2010 23:45:27 +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 Object as a C Structure</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-object-as-a-c-structure.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-object-as-a-c-structure.html#comments</comments>
		<pubDate>Sun, 01 Mar 2009 21:06:46 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=263</guid>
		<description><![CDATA[Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it&#8217;s interesting none-the-less. In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it&#8217;s interesting none-the-less. </p>
<p>In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure with the variables of a specified class.<br />
<span id="more-263"></span></p>
<p>For instance, if we have a class named TestClass, here is how one might create a structure using the @defs directive:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> testClassStructure
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">@defs</span><span style="color: #002200;">&#40;</span>TestClass<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span> <span style="color: #002200;">*</span>testClassAsStruct;</pre></div></div>

<p>Before we look at how to use this structure to access the variables of TestClass, let&#8217;s look at a simple interface and implementation of TestClass:</p>
<p><strong>TestClass Interface</strong></p>

<div class="wp_syntax"><div 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: #400080;">NSString</span> <span style="color: #002200;">*</span>testString;
  <span style="color: #a61390;">int</span>      testInteger;
  <span style="color: #a61390;">BOOL</span>     testBoolean;
<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> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>testString;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readonly<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">int</span> testInteger;
<span style="color: #a61390;">@property</span> <span style="color: #a61390;">BOOL</span> testBoolean;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><strong>TestClass Implementation</strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  TestClass.m</span>
<span style="color: #11740a; font-style: italic;">//  </span>
<span style="color: #6e371a;">#import &quot;TestClass.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*-------------------------------------
* TestClass implementation
*-------------------------------------*/</span>
<span style="color: #a61390;">@implementation</span> TestClass
&nbsp;
<span style="color: #a61390;">@synthesize</span> testString;
<span style="color: #a61390;">@synthesize</span> testInteger;
<span style="color: #a61390;">@synthesize</span> testBoolean;
&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;">&#123;</span>
    testString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fubar&quot;</span><span style="color: #002200;">&#93;</span>;
    testInteger <span style="color: #002200;">=</span> <span style="color: #2400d9;">99</span>;
    testBoolean <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #002200;">&#125;</span>
  <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><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>testString 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>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Pretty simple stuff. Our interface defines three instance variables, a string, an integer and a boolean. The implementation file, upon initialization of a TestClass object sets defaults values for each instance variable.</p>
<p>So, let&#8217;s look at how to use a C structure to read/write the instance variables of TestClass:</p>
<p><strong>AppDelegate Interface</strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UntitledAppDelegate.h</span>
&nbsp;
<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></div></div>

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

<div class="wp_syntax"><div 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: #a61390;">struct</span> testClassStructure
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">@defs</span><span style="color: #002200;">&#40;</span>TestClass<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span> <span style="color: #002200;">*</span>testClassAsStruct;
&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> 
&nbsp;
  TestClass <span style="color: #002200;">*</span>tmp;
&nbsp;
  tmp <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>;
&nbsp;
  testClassAsStruct <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> testClassStructure <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tmp;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testString: %@&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testString<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testInteger: %d&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testInteger<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testBoolean: %s&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testBoolean <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span> ? <span style="color: #bf1d1a;">&quot;Yes&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">&quot;No&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  testClassAsStruct<span style="color: #002200;">-</span>&gt;testString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A new string&quot;</span>;
  testClassAsStruct<span style="color: #002200;">-</span>&gt;testInteger <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
  testClassAsStruct<span style="color: #002200;">-</span>&gt;testBoolean <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testString: %@&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testString<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testInteger: %d&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testInteger<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testBoolean: %s&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testBoolean <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span> ? <span style="color: #bf1d1a;">&quot;Yes&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">&quot;No&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>tmp 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></div></div>

<p>And of course, the output:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/12/cstructasobject.png"/></p>
<p>There you have it. An inside look at how to work with an Objective-C object using a C structure. </p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-object-as-a-c-structure.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Objective-C: Where is My Object Retained?</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-where-is-my-object-retained.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-where-is-my-object-retained.html#comments</comments>
		<pubDate>Fri, 08 Aug 2008 07:08:02 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=232</guid>
		<description><![CDATA[Apologies for the gap since my last post. It&#8217;s been a long week as my wife and I were in a serious car accident a week ago today. I&#8217;ll spare the details, however, let it suffice to say that our vacation plans didn&#8217;t include all the drama that unfolded that day. We are both on [...]]]></description>
			<content:encoded><![CDATA[<p>Apologies for the gap since my last post. It&#8217;s been a long week as my wife and I were in a serious car accident a week ago today. I&#8217;ll spare the details, however, let it suffice to say that our vacation plans didn&#8217;t include all the drama that unfolded that day. We are both on the mend and have plans for a full recovery. Let&#8217;s get back into it&#8230;</p>
<p>For those new to Objective-C and iPhone development, I want to point out something that might save you some time. Look at the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  ..
	UIView <span style="color: #002200;">*</span>containerView;
  ...
<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;">@implementation</span> SomeClass
...
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadView
<span style="color: #002200;">&#123;</span>
  ...
  UIView <span style="color: #002200;">*</span>uiView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView 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> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  self.containerView <span style="color: #002200;">=</span> uiView;
  <span style="color: #002200;">&#91;</span>uiView release<span style="color: #002200;">&#93;</span>;
  ...
<span style="color: #002200;">&#125;</span>
...
<span style="color: #a61390;">@end</span></pre></div></div>

<p>In this block of code, I am setting up a container view, that will hold several subviews. Once the assignment is complete on line 8, I release the local object uiView. Question is, how I can I get away with releasing the variable and still have access to the view in the SomeClass object? In other words, where is the retain?</p>
<p>First thing to notice is that the dot syntax is shorthand for accessing class instance variables using Objective-C properties. If one were to write the code without using properties (and thus, no dot syntax) the answer is obvious.</p>
<p>For example, the equivalent code directly calling the accessor (setter) method would look as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self setContainerView<span style="color: #002200;">:</span>uiView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>uiView release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>At this point, if one was curious about where the object was retained (before the release), the obvious place to look is inside the setter, which might look as follows:</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> setContainerView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> view
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>view retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>containerView release<span style="color: #002200;">&#93;</span>;
  containerView <span style="color: #002200;">=</span> view;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Easy enough, the object is retained as part of the setter. What we have here is a good example of pro&#8217;s/con&#8217;s when working with properties. On one hand, properties allow for simplification and consistency in our code. On the other hand, if using properties and we request the compiler to implement the getter/setter methods (through use of the @synthesize directive) there is code created for us to implement these methods, which we never see.</p>
<p>Although properties are no doubt handy, as this example illustrates, it&#8217;s important to understand what is happening behind the scenes. A clear picture of the little nuances can go a long ways when it comes to debugging a thorny problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-where-is-my-object-retained.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Objective-C: Alternative Use of Properties?</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-alternative-use-of-properties.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-alternative-use-of-properties.html#comments</comments>
		<pubDate>Fri, 25 Jul 2008 06:24:52 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=228</guid>
		<description><![CDATA[While scanning a few code examples inside the iPhone SDK, I bumped into something interesting. I wrote a short example to mimic what I found&#8230;take a look at the following interface declaration: @interface SomeClass : NSObject &#123; NSString *str; NSDate *date; &#125; &#160; @property &#40;nonatomic, retain&#41; NSString *str; @property &#40;nonatomic, retain&#41; NSDate *date; @property &#40;readonly&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>While scanning a few code examples inside the iPhone SDK, I bumped into something interesting. I wrote a short example to mimic what I found&#8230;take a look at the following interface declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date;
<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> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readonly<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>testSomething;</pre></div></div>

<p>The only aspect that should standout is the property declaration for <em>testSomething</em> . Notice there is no instance instance variable tied to this declaration. This is different if for no reason other than the typical use for properties is as a shorthand for declaring accessor methods for instance variables.</p>
<p>So what happens if we now add an instance method, such as shown below, inside the implementation file for <em>SomeClass</em> (and don&#8217;t include either a @synthesize or @dynamic declaration)?</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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> testSomething
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Well, for one thing, we can now access the <em>testSomething</em> method using dot syntax.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> initWithStrAndDate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fubar&quot;</span><span style="color: #002200;">&#93;</span>;
...
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test: %@&quot;</span>, ptr.testSomething<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>This is intriguing. I couldn&#8217;t find any specific references to using properties in this manner. Is this simply a side-effect of how properties are implemented?</p>
<p>If you can shed any light on this, including whether this is a common/good practice, please post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-alternative-use-of-properties.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Objective-C: Properties, Setters and Dot Syntax</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html#comments</comments>
		<pubDate>Thu, 24 Jul 2008 07:08:26 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=227</guid>
		<description><![CDATA[I&#8217;ve been ramping up on iPhone development, and with the NDA still in place (as far as I know), I haven&#8217;t been able to blog about what I&#8217;ve written/learned. And with that, it&#8217;s been quiet in here. Too get back into this, let&#8217;s continue to spend some more time on Objective-C&#8230; The properties feature in [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been ramping up on iPhone development, and with the NDA still in place (as far as I know), I haven&#8217;t been able to blog about what I&#8217;ve written/learned. And with that, it&#8217;s been quiet in here. Too get back into this, let&#8217;s continue to spend some more time on Objective-C&#8230;</p>
<p>The properties feature in Objective-C is your friend. Once you&#8217;ve written a few classes and manually wrote the accessors (getters and setters), you&#8217;ll quickly understand why properties are a good thing.</p>
<p>In addition to automatic creation of getters/setters, there is an option to use dot syntax in place of the traditional <span style="color: #800000;"><em>[receiver message]</em> </span> format.</p>
<p>I want to point out one little nuance when working with <em>properties, setters and dot syntax</em> , that I didn&#8217;t find to be particularly intuitive.</p>
<p>Let&#8217;s say you have a class with an interface definition such as this:</p>
<pre>@interface SomeClass : NSObject
{
  NSString *str;
  NSDate *date;
} 

@property (nonatomic, retain) NSString *str;
@property (nonatomic, retain) NSDate *date;
...</pre>
<p>The implementation file might look like this:</p>
<pre>@implementation SomeClass

@synthesize str;
@synthesize date;

...</pre>
<p>Using this approach, with the combination of @property and @synthesize, we now have getters/setters that are automagically created for you. You can call them as expected</p>
<pre>// Call the getter
NSLog(@&quot;The value is: %s&quot;, [ptr str]);

// Call the setter
[ptr setStr:@&quot;fubar&quot;];</pre>
<p>Using dot syntax, here is what a call to the getter would look like:</p>
<pre>// Call the getter
NSLog(@&quot;The value is: %@&quot;, ptr.str);</pre>
<p>This <a href="http://en.wikipedia.org/wiki/Syntactic_sugar" target="_blank">syntactic sugar</a> is quite handy and easy to grasp, for the most part (even more so if you come from languages such as Java).</p>
<p>Everything is pretty much as expected up to this point. The whole reason for this tip is to callout the syntax for the setter when using dot syntax. Logic tells me, it should look as follows:</p>
<pre>// Call the setter, well on second thought, maybe not
ptr.setStr = @&quot;Testing this&quot;;</pre>
<p>Seems reasonable doesn&#8217;t it? It effectively matches the setter method of the [receiver message] approach. However, the correct syntax is:</p>
<pre>// Call the setter
ptr.str = @&quot;Testing this&quot;;</pre>
<p>The first time I ran across a setter used this way I stopped, scratched my head a few times, scrunched up my face and probably mumbled something along the lines of &quot;what the&#8230;&quot;</p>
<p>As you dig deeper into Objective-C, and even more so, as you look into the examples that are included with the iPhone SDK, you&#8217;ll want to make note of this, as properties are used extensively throughout the code.</p>
<p>The format (for the setter) when using dot syntax takes some getting used to. However, hopefully this little tip will clear up the confusion until you are used to seeing this style of setter.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Objective-C: Private Methods</title>
		<link>http://MacDeveloperTips.com/objective-c/private-methods.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/private-methods.html#comments</comments>
		<pubDate>Wed, 02 Jul 2008 11:35:20 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/objective-c/objective-c-private-methods.html</guid>
		<description><![CDATA[Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here: iPhone Developer . One common complaint for many new to Objective-C is that the language lacks support for private methods within a class. There are [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #993300;"><em><span>Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here:</span> <a href="http://macdevelopertips.com/iphone-developer" target="_blank">iPhone Developer</a> .</em> </span></p>
<p>One common complaint for many new to Objective-C is that the language lacks support for private methods within a class. There are compiler directives for instance variables: @private, @protected (the default) and @public. However, if you want to hide methods, there is no specific directives to help. The example here builds on the previous post on working with categories.</p>
<p>Ultimately, there is no means to make a method private in Objective-C. However, one can hide methods in a class as I show here. It&#8217;s important to understand, this still does not make the methods private, it just makes them &#8220;harder to find&#8221; if you will.</p>
<p>To implement hidden methods (instance and/or class) you can use <em>categories</em> . Let&#8217;s begin with a file called SomeClass.h, the interface definition for SomeClass.</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;">// = File: SomeClass.h</span>
<span style="color: #11740a; font-style: italic;">// = Interface for SomeClass</span>
<span style="color: #11740a; font-style: italic;">// ===========================</span>
&nbsp;
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> Object
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> msg;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> classMsg; 
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Nothing too interesting  here, simply an interface declaration for a class with two methods: <em>msg</em> and <em>classMsg</em> . By definition (of the language) both methods are publicly accessible. Now, let&#8217;s look at the corresponding implementation file shown below. Begin at the bottom where I write the implementation for the two methods above. Again, nothing new, so far. Now, if you start at the top of this file, notice how I&#8217;ve added an interface for <em>SomeClass</em> (similar to the interface definition above), however, I added <span style="color: #800000;"><em>(hidden)</em> </span> to the definition, which now makes the interface definition a category. Just below the definition, I write the code for the two methods in the category.</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;">// = File: SomeClass.m</span>
<span style="color: #11740a; font-style: italic;">// ===========================</span>
<span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// =================================</span>
<span style="color: #11740a; font-style: italic;">// = Interface for hidden methods</span>
<span style="color: #11740a; font-style: italic;">// =================================</span>
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">&#40;</span>hidden<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> hiddenClassMethod;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> hiddenInstanceMethod; 
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// =====================================</span>
<span style="color: #11740a; font-style: italic;">// = Implementation of hidden methods</span>
<span style="color: #11740a; font-style: italic;">// =====================================</span>
<span style="color: #a61390;">@implementation</span> SomeClass <span style="color: #002200;">&#40;</span>hidden<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> hiddenClassMethod
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;Hidden class method.<span style="color: #2400d9;">\n</span>&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> hiddenInstanceMethod
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;Hidden instance method<span style="color: #2400d9;">\n</span>&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #11740a; font-style: italic;">// = Implementation for SomeClass</span>
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> msg
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Inside msg()...<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>self hiddenInstanceMethod<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>SomeClass hiddenClassMethod<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> classMsg
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Inside classMsg()...<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>To see what this does for us, look at the code below, followed by the screenshot:</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;">// = File: Main.m</span>
<span style="color: #11740a; font-style: italic;">// ===========================</span>
<span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
&nbsp;
<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>
  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Display message (including messages from hidden methods)</span>
  <span style="color: #002200;">&#91;</span>ptr msg<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Call a class method</span>
  <span style="color: #002200;">&#91;</span>SomeClass classMsg<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Compile warning (can't access hidden instance method)</span>
<span style="color: #11740a; font-style: italic;">//  [ptr hiddenInstanceMethod];</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Compile warning (can't access hidden class method)</span>
<span style="color: #11740a; font-style: italic;">//  [SomeClass hiddenClassMethod];  </span>
&nbsp;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/private1.png" alt="" /></p>
<p>Notice how I can access the hidden instance and class methods from within the class. However, if I remove the comments from lines 17 and 20, to attempt access to the hidden methods, the compiler generates the following warnings:</p>
<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/private2.png" alt="" /></p>
<p>I&#8217;m not sure if this is the only means to hide methods. If you know of another way (and have a code example), please post a comment.</p>
<p>The output of the application is shown here:</p>
<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/private3.png" alt="" /></p>
<p><a href="http://macdevelopertips.com/wp-content/uploads/2008/06/PrivateMethods.zip">Download the code</a> and give this a try within Xcode.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/private-methods.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Objective-C: Categories</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-categories.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-categories.html#comments</comments>
		<pubDate>Tue, 01 Jul 2008 11:21:09 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[categories]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=221</guid>
		<description><![CDATA[As an alternative to subclassing, Objective-C categories provide a means to add methods to a class. What&#8217;s intriguing, is that any methods that you add through a category become part of the class definition, so to speak. In other words, if you add a method to the NSString class, any instance, or subclass, of NSString [...]]]></description>
			<content:encoded><![CDATA[<p>As an alternative to subclassing, Objective-C categories provide a means to add methods to a class. What&#8217;s intriguing, is that any methods that you add through a category become part of the class definition, so to speak. In other words, if you add a method to the NSString class, any instance, or subclass, of NSString will have access to that method.</p>
<p>Defining a category is identical to defining the interface for a class, with one small exception: you add a category name inside a set of parenthesis after the interface declaration. The format is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> ClassToAddMethodsTo <span style="color: #002200;">&#40;</span>category<span style="color: #002200;">&#41;</span>
  ...methods go here
<span style="color: #a61390;">@end</span></pre></div></div>

<p>For example, below I&#8217;ve defined a category that adds a method to the NSString class. The method <em>reverseString</em> adds the capability to all NSString objects to reverse the characters in the string.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>reverse<span style="color: #002200;">&#41;</span>
<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> reverseString;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>As with the @interface declaration, the @implementation section changes only in that the category name is added to the definition. Below is the implementation of the interface defined above. Notice how in both cases I added <em>(reverse)</em> , which is the category name I assigned.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>reverse<span style="color: #002200;">&#41;</span>
&nbsp;
<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> reverseString
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>reversedStr;
  <span style="color: #a61390;">int</span> len <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self length<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Auto released string</span>
  reversedStr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithCapacity<span style="color: #002200;">:</span>len<span style="color: #002200;">&#93;</span>;     
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Probably woefully inefficient...</span>
  <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>len &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>reversedStr appendString<span style="color: #002200;">:</span>
         <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%C&quot;</span>, <span style="color: #002200;">&#91;</span>self characterAtIndex<span style="color: #002200;">:--</span>len<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;   
&nbsp;
  <span style="color: #a61390;">return</span> reversedStr;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>What follows is a short example to showing how one might use the above category.</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>
<span style="color: #6e371a;">#import &quot;NSString+Reverse.h&quot;</span>
&nbsp;
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <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: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fubar&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>rev;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;String: %@&quot;</span>, str<span style="color: #002200;">&#41;</span>;
  rev <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str reverseString<span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reversed: %@&quot;</span>,rev<span style="color: #002200;">&#41;</span>; 
&nbsp;
  <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The output of the above example is shown here:</p>
<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/category.png" alt="" /></p>
<p><strong>Overriding Methods</strong></p>
<p>Categories can also be used to override methods the class inherits, again, providing an alternative to subclassing. You can always access the overridden method using <em>super</em> . I would assume from an internal (compiler/code generation) perspective, this could lead to less code/overhead as compared to creating a subclass solely to override a method (caveat: I have no proof that is true).</p>
<p><strong>Dividing Source Code into Separate File</strong></p>
<p>Although I haven&#8217;t given this a go, categories provide an interesting opportunity to disperse the implementation of a class across one or more files. The Objective-C Programming Guide lists several benefits of this including:</p>
<ul>
<li>Opportunity to group together methods that perform similar tasks.</li>
<li>Configuring classes differently for various applications, yet maintaining one set of code.</li>
</ul>
<p><strong>Naming Conventions</strong></p>
<p>The recommended naming convention for a category is &#8220;ClassToAddMethodsTo+CatgoryName&#8221; for instance, I used the following filenames for the interface and implementation of the above category code:</p>
<ul>
<li>NSString+Reverse.h</li>
<li>NSString+Reverse.m</li>
</ul>
<p><strong>Caveats</strong></p>
<p>You cannot add instance variables to a class through a category. Also, category names must be unique across an application.</p>
<p><strong>Source Code</strong></p>
<p>I&#8217;ve <a href="http://iphonedevelopertips.com/wp-content/uploads/2008/06/Categories.zip">attached the Xcode project</a> for the above example if you&#8217;d like to give it a try.</p>
<p>In the next post I&#8217;ll show another example of using categories &#8211; the example will show one approach for hiding methods within in a class, as Objective-C does not offer support for private methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-categories.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Objective-C: Initializers</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-initializers.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-initializers.html#comments</comments>
		<pubDate>Mon, 30 Jun 2008 07:17:07 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[initializers]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=223</guid>
		<description><![CDATA[Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here: iPhone Developer . The code for creating new instances of a class generally looks as follows: SomeClass *ptr = [[SomeClass alloc] init]; In this case, [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #993300;"><em><span>Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here:</span> <a href="http://macdevelopertips.com/iphone-developer" target="_blank">iPhone Developer</a> .</em> </span></p>
<p>The code for creating new instances of a class generally looks as follows:</p>
<pre>  SomeClass *ptr = [[SomeClass alloc] init];
</pre>
<p>In this case, we send a message (alloc) to the recevier (SomeClass) to create a new instance; the object returned from this message, then sends a message (init) to initialize instance variables within the class.</p>
<p>I briefly talked about the importance of the order in which objects are allocated and initialized. You can read more about that here: <a href="http://macdevelopertips.com/objective-c/objective-c-memory-management.html">Memory Management in Objective-C</a> . In this post, I want to introduce how to override the <em>init</em> method of the NSObject class, more specifically, how to work with multiple initializers.</p>
<p>In the <em>init</em> method of the NSObject class, no initialization takes place, it simply returns <em>self</em> .</p>
<p>The basic format of a method to override the initializer looks as follows:</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;">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;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Initialization code here</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
  <span style="color: #002200;">&#125;</span></pre></div></div>

<p>It&#8217;s important to call the super init method first to ensure that instance variables up the inheritance hierarchy are initialized in the proper order (from top to bottom). Also, if your initialize code fails, you need to return <em>nil</em> from the overriden <em>init</em> method. Finally, return a reference to <em>self</em> as shown above.</p>
<p>So let&#8217;s assume we have a block of code as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Testing&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here we initialize a new instanc of SomeClass, and follow this with a call to set an instance variable to the specified string (@&#8221;Testing). One common means to accomplish this is to create a new initializer in which we pass in the parameter (the string in this case) as part of the original creation of the object. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> initWithStr<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Testing&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>We can also take this one step further. Let&#8217;s say that we also wanted to initialize an instance variable that was a pointer to a date object (NSDate *). In that case, we might want an additional initilizer that looks as follows:</p>
<pre>  SomeClass *ptr = [[SomeClass alloc] initWithStrAndDate:@"Testing"
       date:[NSDate date]];
</pre>
<p>It quite common for classes to have more than one initializer for creating new objects, allowing variations as shown above. This also implies that the initialization methods need to work in harmony. Designated initializers are the means to achieve this harmonious state.</p>
<p><strong>Designated Initializers</strong><br />
When working with a class that has more than one initialization method (as shown above), it&#8217;s important that one of the initializers drives the whole process. Put another way, only one of the initializer methods does the actual work of calling the super class initializer and initializing the instance variables of the object.</p>
<p>This process is actually quite simple. Let&#8217;s assume we have a class implementation as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
  <span style="color: #002200;">&#123;</span>
  	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date;
  <span style="color: #002200;">&#125;</span> 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Designated initializer</span>
  <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithStrAndDate<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>inStr date<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>inDate;
  <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithStr<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>inString;
  <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init;</pre></div></div>

<p>The way this should work, is that a call to the initializer <em>init</em> should call <em>initWithStr</em> . A call to the initializer <em>initWithStr</em> should call <em>initWithStrAndDate</em> . Following this process, all the actual initialization work occurs in <em>initWithStrAndDate</em> . This method (the one that does the work) is known as the <strong>designated initializer</strong> .</p>
<p>A general rule of thumb (although not always the case) is that the designated initializer is the initializer with the most parameters.</p>
<p>So let&#8217;s see how this might look within the implementation of a class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
  <span style="color: #11740a; font-style: italic;">// ==========================</span>
  <span style="color: #11740a; font-style: italic;">// = Designated initializer =</span>
  <span style="color: #11740a; font-style: italic;">// ==========================</span>
  <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithStrAndDate<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>inString date<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>inDate
  <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;">&#123;</span>
      <span style="color: #002200;">&#91;</span>self setStr<span style="color: #002200;">:</span>inString<span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span>self setDate<span style="color: #002200;">:</span>inDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <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><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithStr<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>inString
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Either of these will work</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self initWithStrAndDate<span style="color: #002200;">:</span>inString date<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">//  return [self initWithStrAndDate:inString date:nil];</span>
  <span style="color: #002200;">&#125;</span>
&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;">return</span> <span style="color: #002200;">&#91;</span>self initWithStr<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  ...
  <span style="color: #a61390;">@end</span></pre></div></div>

<p>Notice how starting with <em>init</em> , it calls the next initializer in the chain, <em>initWithStr</em> , passing in a default value (in this case, nil). This method then calls the designated initializer, again, passing in a default value, this time for the date. And notice how the designated initializer is the only method that calls <em>super</em> .</p>
<p>Working with multiple initializers is a simple process of ensuring that each initializer calls up through the initialization chain within the class. This ensures that all instance variables are initialized in just one place, and that the super method is called such that all instance variables of classes further up the hierarchy are initialized first (from top down).</p>
<p>Here is a copy of the <a href="http://macdevelopertips.com/wp-content/uploads/2008/06/Initializers.zip">Initializers Xcode project</a> if would like to try the above example within Xcode.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-initializers.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Objective-C: Memory Management</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-memory-management.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-memory-management.html#comments</comments>
		<pubDate>Thu, 26 Jun 2008 07:08:02 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[memory management]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=218</guid>
		<description><![CDATA[Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. More information about writing for the iPhone can be found here: iPhone Developer Tips . First off, it&#8217;s worthing clarifying that much of what is covered here is as much Cocoa as [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #993300;"><em><span>Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. More information about writing for the iPhone can be found here:</span> <a href="http://iphonedevelopertips.com/objective-c/memory-management.html" target="_blank">iPhone Developer Tips</a> .</em> </span></p>
<p>First off, it&#8217;s worthing clarifying that much of what is covered here is as much Cocoa as it is Objective-C. However, since the two are quite closely tied when it comes to developing Mac/iPhone applications, I&#8217;ve decided to keep with the Objective-C theme as it relates to the title of this and subsequent posts.</p>
<p>This post will review some of the nuances of memory management. I won&#8217;t go into all the details of  memory management (see <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html" target="_blank">Objective-C Programming Guide</a> , <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf" target="_blank">Cocoa Fundamentals</a> and <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.pdf" target="_blank">Memory Management Programming Guide for Cocoa</a> ), rather, I&#8217;ll point out some of the more subtle concepts to keep in mind as you begin to work with objects.</p>
<p><strong>Object Ownership</strong></p>
<p>Rule #1 &#8211; If you create an object using alloc or copy, you need to free that object.<br />
Rule #2 &#8211; If you didn&#8217;t create an object directly, don&#8217;t attempt to release the memory for the object.</p>
<p>For instance, here is an object that I &#8220;own&#8221; given that I&#8217;ve called &#8216;alloc&#8217; to create the object. It&#8217;s up to me to release the memory for this object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  TestClass <span style="color: #002200;">*</span>ptr <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>;
&nbsp;
  ...
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Let&#8217;s look at two examples where we are not responsible for managing the memory associated with an object. First, when using a factory (aka convenience) methods of a class. For instance:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Some string here...&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span>str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The second example, is if you call an accessor method that returns an object. Assume that the object TestClass below has a getter method that returns a pointer to an instance variable named firstName that is an NSString.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
  <span style="color: #11740a; font-style: italic;">// No need to release this object</span>
  str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>TestClass firstName<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This makes sense if you think about it. The getter simple returns a reference (pointer) to an existing object. Now, if you want to retain (express an interest in the object to keep it around) then you would need to use &#8216;retain&#8217; to bump the reference count. However, if we simply want a pointer to the object as shown here, we don&#8217;t have responsibility to release the object.</p>
<p><strong>Initializing Objects</strong></p>
<p>The order of things happening when initializing an object is of great relevance. For example, look at this block of code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  TestClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>TestClass alloc<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr init<span style="color: #002200;">&#93;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Do something with the object</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>ptr<span style="color: #002200;">&#41;</span>
    ...</pre></div></div>

<p>Seems harmless enough, right? Allocate an object and send a message to initialize the object. Here&#8217;s the problem, if the init method returns nil (which all good initializers do upon failure to properly initialize an object), the code on line 5 would pass the test (that is, ptr would not be nil).</p>
<p>Now compare that with this approach:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  TestClass <span style="color: #002200;">*</span>ptr <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>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Do something with the object</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>ptr<span style="color: #002200;">&#41;</span>
    ...</pre></div></div>

<p>In the code above, assuming the init method returns nil upon failure, ptr will be set to nil, and the code on line 4 would not pass the test. Much better.</p>
<p>So, the lesson here is twofold: always return nil from an initializer method when the method fails to properly initialize the object. Second, it&#8217;s a good practice to combine a call to alloc and init into one step. We&#8217;ll talk more about initializers in a separate post.</p>
<p><strong>Releasing Objects</strong></p>
<p>Let&#8217;s talk about the other end, releasing objects. Have a look at the implementation for a simple class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><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;">// = Implementation for TestClass =</span>
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #a61390;">@implementation</span> TestClass
&nbsp;
<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> str
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> str;
<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> setStr<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>input
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>input retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
  str <span style="color: #002200;">=</span> input;
<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>str 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>

<p>Look at the code below that creates an instance of the TestClass, calls the setter method to initialize the &#8216;str&#8217; instance variable, and releases the memory for the &#8216;ptr&#8217; object.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;TestClass.h&quot;</span>
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <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: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  TestClass <span style="color: #002200;">*</span>ptr <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>;
  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fubar&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The <em>dealloc</em> method in TestClass releases the memory for the &#8216;str&#8217; instance variable, and it seems all is well. However, if at some point, somewhere inside the class implementation an attempt was made to check if the &#8216;str&#8217; instance variable is <em>nil</em> (prior to doing some operation) for example:</p>
<pre>if (str)
  do something...
else
  do something else...</pre>
<p>the if statement would pass and the code on line 2 would be run. Not a good thing.</p>
<p>A better way is to replace the call to <em>release</em> with a call to the setter method and pass in <em>nil</em> . Look at the <em>dealloc</em> method below:</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> setStr<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>input
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>input retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
  str <span style="color: #002200;">=</span> input;
<span style="color: #002200;">&#125;</span>
&nbsp;
...
&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>self setStr<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><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></pre></div></div>

<p>The reason this works is that you can send a message to a <em>nil</em> object without raising an exception. If you follow the trail you&#8217;ll see inside the <em>setStr</em> method that the variable &#8216;input&#8217; (which is nil) is sent a message to <em>retain</em> . This is effectively ignored, as the object is <em>nil</em> . The next line releases the string &#8216;str&#8217;. The last line sets &#8216;str&#8217; to the value of the input parameter (which is <em>nil</em> ). This prevents the problem in the previous example where &#8216;str&#8217; still had a reference to a location in memory (even though the memory had been released).</p>
<p>As I was getting familiar with this, I wrote a short example in Xcode. Here is the test block for the <em>dealloc</em> method:</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> dealloc
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Address of 'str' before release is: %ld&quot;</span>, str<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// After this, 'str' still points to a memory location</span>
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// After this, 'str' will be nil  (** This is the preferred approach **)</span>
  <span style="color: #11740a; font-style: italic;">//[self setStr:nil];</span>
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Address of 'str' is after release is: %ld&quot;</span>, str<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Notice the result of this based on how the instance var 'str' is released (above)</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>str <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;'str' still points to a memory location (after being released)!&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;'str' now points to nil.&quot;</span><span style="color: #002200;">&#41;</span>;        
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Notice in this case that line 6 uses release. See the output below:</p>
<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/memory-mgmt1.png" alt="" /></p>
<p>Now, if you comment out line 6 and remove the comment from line 9, you&#8217;ll get the following output. Notice how the reference to &#8216;str&#8217; after is 0 (nil).</p>
<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/memory-mgmt2.png" alt="" /></p>
<p>You can <a href="http://macdevelopertips.com/wp-content/uploads/2008/06/MemoryManagement.zip">download the Xcode project</a> here if you&#8217;d like to take a closer look at the complete example. Hopefully this information will save you some time trying to track down an elusive memory leak.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-memory-management.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Objective-C: Messaging</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-messaging.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-messaging.html#comments</comments>
		<pubDate>Tue, 24 Jun 2008 06:06:39 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[messaging]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=214</guid>
		<description><![CDATA[Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here: iPhone Developer . This post introduces messaging within Objective-C. Messaging is the terminology for invoking methods on an object. The format for a message expression [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #993300;"><em><span>Note: What follows is another post in an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here:</span> <a href="http://macdevelopertips.com/iphone-developer" target="_blank">iPhone Developer</a> .</em> </span></p>
<p>This post introduces messaging within Objective-C. Messaging is the terminology for invoking methods on an object. The format for a message expression is as follows (the brackets are required):</p>
<p><span style="color: #800000;"><em>[object method] </em> </span></p>
<p>or in Objective-C parlance</p>
<p><em><span style="color: #800000;">[receiver message]</span> </em></p>
<p>Here&#8217;s a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #11740a; font-style: italic;">// Create an instance of SomeClass object</span>
  <span style="color: #11740a; font-style: italic;">// We'll cover this later...</span>
  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Send the message 'printInstanceVars' to the 'ptr' receiver</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If we want to pass an argument as part of the message (that is, pass a parameter to the method), we send a message that looks like this:</p>
<p><em><span style="color: #800000;">[receiver message:argument]</span> </em></p>
<p>For example, assume <em>setStr</em> and <em>setX</em> are two methods in the <em>SomeClass</em> object and <em>ptr</em> is a pointer an instance of <em>SomeClass</em> . Here is how we might pass arguments to each method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Testing&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr setX<span style="color: #002200;">:</span><span style="color: #2400d9;">2008</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Side note: The @ symbol at the front of @&#8221;Testing&#8221; string is convenience method that converts the given string to an <em>NSString</em> object, which in the case of the <em>setStr</em> method, is the required type for the parameter.</p>
<p><strong>Nest Messages</strong><br />
We can also nest messages, as shown below. In this example, we first pass a message to the <em>NSDate</em> object. This is a class object with a factory method &#8216;date&#8217; for creating a new <em>NSDate</em> object. If that makes no sense, don&#8217;t worry about it. Think of it as nothing more than creating a new <em>NSDate</em> object that holds the current date and time. The return value of inner message (the <em>NSDate</em> object) is the argument for <em>setDate</em> message. What we&#8217;ve done here is to create a new date object and pass it as an argument to the <em>setDate</em> message of the receiver (<em>ptr</em> ). Sounds more confusing than it is.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>ptr setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><strong>Multiple Arguments</strong><br />
Taking this another step further, we can pass multiple arguments along with our message. The message below takes three arguments, a string, date and integer. Sometimes it easier to read the method aloud to get this jist of what&#8217;s up. In this case, &#8220;pass a message to the ptr receiver that sets a string, and a date and an integer.&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A new test...&quot;</span> andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> andInteger<span style="color: #002200;">:</span><span style="color: #2400d9;">99</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here is how we define the message in the interface file:</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> setStr<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>str andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date andInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>x;</pre></div></div>

<p>And here is the implementation of the setStr() method which accepts three arguments:</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> setStr<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>strInput andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dateInput
    andInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>xInput
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self setStr<span style="color: #002200;">:</span>strInput<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self setDate<span style="color: #002200;">:</span>dateInput<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self setX<span style="color: #002200;">:</span>xInput<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Let me point out a few things about this method. First, notice how in the definition I used the names <em>str</em> , <em>date</em> and <em>x</em> . However, in the implementation, I used the names <em>strInput</em> , <em>dateInput</em> and <em>xInput</em> . The first thing to understand is that you can do this. The reason for doing so, is that in the class where <em>setStr()</em> is defined, I have instance variables with the names <em>str</em> , <em>date</em> and <em>x</em> . If I didn&#8217;t change the names in the actual implementation, I&#8217;d get a compiler warning that a local declaration hides an instance variable.</p>
<p>In this code example, the simplest thing to do is to change the variable names in the method to make it obvious what variables are referring to what. This may not be the best naming scheme for your applications, however, I wanted to point out the flexibility as to how you define and implement methods and their arguments. Obviously, do what makes the most sense for your specific needs and at the same time, results in the most readable code.</p>
<p>Also, note that this version of <em>setStr()</em> does nothing more than send a message as follows: the receiver is current object instance (self), the message is a setter method and the argument to each message is the appropriate parameter passed in to <em>setStr()</em> .</p>
<p>Using the format/syntax of Objective-C takes some getting used to. However, once you do, you&#8217;ll find that it&#8217;s easy to read code that has had some thought put into the names of message and the parameters.</p>
<p><strong>Variable Number of Arguments</strong><br />
The last topic is creating messages with a variable number of arguments. Let&#8217;s start with the interface definition of a method that accepts a variable number of arguments:</p>
<pre>-(void) printInstanceVars:(id)input, ...;</pre>
<p>In this example, I declare on parameter that is of type &#8216;id&#8217;, which can represent any object.  The implementation of the method looks as follows:</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> printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>input, ...
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">id</span> currentObject;
  <span style="color: #a61390;">va_list</span> argList;
  <span style="color: #a61390;">int</span> objectCount <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>input<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;<span style="color: #2400d9;">\n</span> Object #%d is: %@<span style="color: #2400d9;">\n</span>&quot;</span>, objectCount<span style="color: #002200;">++</span>, input<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">va_start</span><span style="color: #002200;">&#40;</span>argList, input<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>currentObject <span style="color: #002200;">=</span> <span style="color: #a61390;">va_arg</span><span style="color: #002200;">&#40;</span>argList, <span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
      NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span> Object #%d is: %@<span style="color: #2400d9;">\n</span>&quot;</span>, objectCount<span style="color: #002200;">++</span>, currentObject<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">va_end</span><span style="color: #002200;">&#40;</span>argList<span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This method will print out each argument using NSLog(). The assumption is made that the last argument will be nil. Here is how you might call the method, passing in two objects. What&#8217;s happening here is that I am sending getter messages, if you will, to the &#8216;ptr&#8217; receiver. Each of these getters returns an object. Notice the nil value as the last parameter.</p>
<pre>  [ptr printInstanceVars:[ptr str], [ptr date], nil];</pre>
<p>Now, this example is a little contrived in that there is probably little value in passing in instance variables from an object, as I&#8217;ve done here. However, introducing more classes at this point may confuse more than help. If nothing else, hopefully you get the jist of how this works.</p>
<p>I haven&#8217;t come upon the case where this is necessary in an Objective-C application, however, when programming in C, this was quite handy when reading command line parameters. So,if you ever find the opportunity, now you&#8217;ll know how. And if you are into trivia, variable argument methods in Objective-C are referred to as variadic methods, go figure.</p>
<p>Many of the things that I&#8217;ve covered here are much better understood by looking at a real example. I&#8217;ve attached the Xcode project that I created to learn this stuff. You best bet is to download, open the project and mess around with the code.</p>
<p>Here is the main method of the example, so you can get an idea of how I tested each of the above examples.</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;">const</span> <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: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Simple message</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Passing a single argument</span>
  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Testing&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr setX<span style="color: #002200;">:</span><span style="color: #2400d9;">2008</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Nesting of messages</span>
  <span style="color: #002200;">&#91;</span>ptr setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Passing multiple arguments</span>
  <span style="color: #002200;">&#91;</span>ptr setString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A new test...&quot;</span> andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> andInteger<span style="color: #002200;">:</span><span style="color: #2400d9;">99</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Passing variable number of argument</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ptr str<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>ptr date<span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// This won't work...</span>
<span style="color: #11740a; font-style: italic;">//    [ptr printInstanceVars:[ptr str], [ptr date], [ptr x], nil];</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ptr str<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>ptr date<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ptr x<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Download the Xcode Project</strong><br />
Download the <a href="http://MacDeveloperTips.com/wp-content/uploads/2008/06/Messaging.zip">Xcode project</a> and take some time to tinker.</p>
<p>Once you&#8217;ve got a good handle on the overall application, look at line 27 above. Make any sense? Here&#8217;s the deal: the variable length method is expecting objects to be passed in. The code in line 26 will generate a runtime error given the message [ptr x] returns an integer (not an object). The way around this is to create an NSNumber object by sending a message to the NSNumber receiver, with the message &#8216;numberWithInt&#8217; passing in as the parameter the integer returned from the getter message sent to the &#8216;ptr&#8217; receiver. If all that makes sense, you&#8217;re well on your way :)</p>
<p>For completeness, the entire code listing and a screenshot are shown 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;">// = SomeClass.h =</span>
<span style="color: #11740a; font-style: italic;">// ===========================</span>
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date;
  <span style="color: #a61390;">int</span> x;
<span style="color: #002200;">&#125;</span> 
&nbsp;
<span style="color: #11740a; font-style: italic;">// Getters</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> x;
<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> str;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> date;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setters</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setStr<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>input;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>input;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setX<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>input;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setStr<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>str andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date andInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>x;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Other</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> printInstanceVars;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>input, ...;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc; 
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
<span style="color: #6e371a;">#import &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #11740a; font-style: italic;">// = SomeClass.m =</span>
<span style="color: #11740a; font-style: italic;">// ================================</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> SomeClass
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #11740a; font-style: italic;">// = Getter methods =</span>
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> x
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> x;
<span style="color: #002200;">&#125;</span>
&nbsp;
<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> str
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> str;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> date
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> date;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #11740a; font-style: italic;">// = Setter Methods =</span>
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setStr<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>foo
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>foo retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
  str <span style="color: #002200;">=</span> foo;
<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> setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>input
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>input retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>date release<span style="color: #002200;">&#93;</span>;
  date <span style="color: #002200;">=</span> input;
<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> setX<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>input
<span style="color: #002200;">&#123;</span>
  x <span style="color: #002200;">=</span> input;
<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> setStr<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>strInput andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dateInput andInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>xInput
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self setStr<span style="color: #002200;">:</span>strInput<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self setDate<span style="color: #002200;">:</span>dateInput<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self setX<span style="color: #002200;">:</span>xInput<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #11740a; font-style: italic;">// = Print the instance vars =</span>
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> printInstanceVars
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Use the getter method of the ’self’ object to print object instance variables</span>
  <span style="color: #11740a; font-style: italic;">//  NSLog(@&quot;\n x: %d\n str: %@\n date: %@\n&quot;, [self x], [self str], [self date]);</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// The class can directly access the instance variables (versus calling message as above)</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span> x: %d<span style="color: #2400d9;">\n</span> str: %@<span style="color: #2400d9;">\n</span> date: %@<span style="color: #2400d9;">\n</span>&quot;</span>, x, str, date<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> printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>input, ...
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">id</span> currentObject;
  <span style="color: #a61390;">va_list</span> argList;
  <span style="color: #a61390;">int</span> objectCount <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>input<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;<span style="color: #2400d9;">\n</span> Object #%d is: %@<span style="color: #2400d9;">\n</span>&quot;</span>, objectCount<span style="color: #002200;">++</span>, input<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">va_start</span><span style="color: #002200;">&#40;</span>argList, input<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>currentObject <span style="color: #002200;">=</span> <span style="color: #a61390;">va_arg</span><span style="color: #002200;">&#40;</span>argList, <span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
      NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span> Object #%d is: %@<span style="color: #2400d9;">\n</span>&quot;</span>, objectCount<span style="color: #002200;">++</span>, currentObject<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">va_end</span><span style="color: #002200;">&#40;</span>argList<span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ====================================</span>
<span style="color: #11740a; font-style: italic;">// = Dealloc all object instance vars =</span>
<span style="color: #11740a; font-style: italic;">// ====================================</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: #11740a; font-style: italic;">// No release needed of the integer instance variable ‘x’</span>
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>date 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>
<span style="color: #a61390;">@end</span></pre></div></div>


<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;">// = Messaging.m =</span>
<span style="color: #11740a; font-style: italic;">// ===========================</span>
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
<span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <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: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Simple message</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Passing a single argument</span>
  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Testing&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr setX<span style="color: #002200;">:</span><span style="color: #2400d9;">2008</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Nesting of messages</span>
  <span style="color: #002200;">&#91;</span>ptr setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Passing multiple arguments</span>
  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A new test...&quot;</span> andDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> andInteger<span style="color: #002200;">:</span><span style="color: #2400d9;">99</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Passing variable number of argument</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ptr str<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>ptr date<span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// This won't work...</span>
  <span style="color: #11740a; font-style: italic;">//    [ptr printInstanceVars:[ptr str], [ptr date], [ptr x], nil];</span>
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ptr str<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>ptr date<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ptr x<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/messaging.png" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-messaging.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Objective-C: Defining a Class</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-defining-a-class.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-defining-a-class.html#comments</comments>
		<pubDate>Mon, 23 Jun 2008 07:08:01 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=209</guid>
		<description><![CDATA[Note: This post is the start of an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here: iPhone Developer . One of the first topics to cover when learning to develop native iPhone applications is how to code in Objective-C. Apple offers [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #993300;"><em><span>Note: This post is the start of an on-going series for developers who are interested in learning to write applications for the iPhone. The entire series can be found here:</span> <a href="http://macdevelopertips.com/iphone-developer" target="_blank">iPhone Developer</a> .</em> </span></p>
<p>One of the first topics to cover when learning to develop native iPhone applications is how to code in Objective-C. Apple offers the <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/index.html" target="_blank">Objective C Reference</a> , a good resource, however, the best way to learn is by writing code. I took to Xcode to write a few simple examples, you&#8217;ll find the code below. At the end of this post I also include a link to download the Xcode project I was working with.</p>
<p>There are two aspects to a class, the interface and the implementation, both of which I recommend you store in separate files (although this is not a requirement).</p>
<p>The interface looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> NameOfClass <span style="color: #002200;">:</span> NameOfSuperclass
<span style="color: #002200;">&#123;</span>
  instance variables here...
<span style="color: #002200;">&#125;</span>
class methods
instance methods
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The interface for my example:</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;">// = Interface for SomeClass =</span>
<span style="color: #11740a; font-style: italic;">// ===========================</span>
&nbsp;
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str;
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date;
  <span style="color: #a61390;">int</span> x;
<span style="color: #002200;">&#125;</span> 
&nbsp;
<span style="color: #11740a; font-style: italic;">// Getters</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> x;
<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> str;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> date;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setters</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#41;</span>setX<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> input;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setStr<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>input;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>input;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Other</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> printInstanceVars;
<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: #a61390;">@end</span></pre></div></div>

<p>A good coding practice is to save the implementation definition in a file with a name that matches the class name, with an extension of .h (for exampe: SomeClass.h).</p>
<p>This class is inherited from NSObject, the uber object. The class has three instance variables, two that point to other objects, one that references an integer variable. Take note of the getter methods: in Objective-C there is typically no &#8216;get&#8217; in the front of the method name (in Java this might look like getX or getStr). Second, it should be obvious, that an instance variable can have the same name as a method, as it generally does with a getter. The &#8216;-&#8217; in the front of the definition, signifies that the method is an instance method. We use a &#8216;+&#8217; to define a class method (more on class methods in a future post).</p>
<p>One important thing to point out is the format used when declaring methods. For example, <span style="color: #800000;"><em>setStr()</em> </span> is defined as <span style="color: #800000;"><em>-(void)setStr: (NSString *) input</em> </span> ; This is translated to, the method setStr is an instance method (given the &#8216;-&#8217;)  that returns a void type. The method takes one argument,  that is a pointer to an NSString object, the name assigned to the parameter is &#8216;input&#8217;. The reason for the name will become more apparent when you see the implementation of the method below.</p>
<p>The format for the implementation of a class looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> NameOfClass <span style="color: #002200;">:</span> NameOfSuperclass
<span style="color: #002200;">&#123;</span>
  instance variables here...
<span style="color: #002200;">&#125;</span>
class methods
instance methods
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Here is how the implementation for the above class looks:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
<span style="color: #6e371a;">#import &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #11740a; font-style: italic;">// = Implementation for SomeClass =</span>
<span style="color: #11740a; font-style: italic;">// ================================</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> SomeClass
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #11740a; font-style: italic;">// = Getter methods =</span>
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> x
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> x;
<span style="color: #002200;">&#125;</span>
&nbsp;
<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> str
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> str;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> date
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> date;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #11740a; font-style: italic;">// = Setter Methods =</span>
<span style="color: #11740a; font-style: italic;">// =================</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setX<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>input
<span style="color: #002200;">&#123;</span>
  x <span style="color: #002200;">=</span> input;
<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> setStr<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>input
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>input retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
  str <span style="color: #002200;">=</span> input;
<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> setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>input
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>input retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>date release<span style="color: #002200;">&#93;</span>;
  date <span style="color: #002200;">=</span> input;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ================================</span>
<span style="color: #11740a; font-style: italic;">// = Print the instance vars =</span>
<span style="color: #11740a; font-style: italic;">// ================================</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> printInstanceVars
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Use the getter method of the 'self' object to print object instance variables</span>
<span style="color: #11740a; font-style: italic;">//  NSLog(@&quot;\n x: %d\n str: %@\n date: %@\n&quot;, [self x], [self str], [self date]);</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// The class can directly access the instance variables (versus calling message as above)</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span> x: %d<span style="color: #2400d9;">\n</span> str: %@<span style="color: #2400d9;">\n</span> date: %@<span style="color: #2400d9;">\n</span>&quot;</span>, x, str, date<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ====================================</span>
<span style="color: #11740a; font-style: italic;">// = Dealloc all object instance vars =</span>
<span style="color: #11740a; font-style: italic;">// ====================================</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: #11740a; font-style: italic;">// No release needed of the integer instance variable 'x'</span>
  <span style="color: #002200;">&#91;</span>str release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>date 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>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Other than learning the syntax of Objective-C, if you are familiar with OO development, most of this should be pretty clear.</p>
<p>A couple of things to point out:</p>
<ul>
<li>The preferred file name for the implementation is the class name with a .m extension, in this example: SomeClass.m</li>
<li>Notice how this file imports &#8220;SomeClass.h&#8221; to read the class definition. If you are familiar with C, this is analgous to the #include directive. The benefit of #import is that the compiler will do the work for you to verify that the include file is only read once. If you&#8217;ve done any amount of coding in C, you&#8217;ll appreciate this convenience, if not, you won&#8217;t understand how nice a feature this is.</li>
<li>Within an instance method, all instance variables are within scope. For example, notice how the getter and setter methods refer to the instance variables.</li>
<li>Notice in printInstanceVars() method that there are two means to access the instance variables. You can use the &#8216;self&#8217; object an send a message to the getter method (more on objects and messages in the next post), or you can directly access the instance variables.</li>
<li>If instance variables are pointers to objects, as are &#8216;str&#8217; and &#8216;date&#8217;, it&#8217;s your responsibility as the developer to free the memory for those objects. The dealloc method is where you do this work. More on that to come&#8230;</li>
</ul>
<p>To complete the example, the code that follows declares an instance of the SomeClass object, and uses the setter/getter methods to print the instance variables to the console.</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>
<span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <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: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  SomeClass <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr setX<span style="color: #002200;">:</span><span style="color: #2400d9;">99</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr setStr<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Testing&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr setDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>ptr printInstanceVars<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>ptr release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>A few comments on the above code:</p>
<ul>
<li>Notice this file imports the SomeClass.h interface file.</li>
<li>Like working with C, main() is the function that gets everything started.</li>
<li>&#8216;ptr&#8217; is a reference to an object of the SomeClass class.</li>
<li>Calling instance methods of an object follows this form: [object message:parameters]</li>
</ul>
<p>A screenshot of the output from within Xcode of this example is below:</p>
<p><img src="http://MacDeveloperTips.com/wp-content/uploads/2008/06/creating-classes2.png" alt="Creating Classes" /></p>
<p>I recommend you <a href="http://MacDeveloperTips.com/wp-content/uploads/2008/06/CreatingClasses.zip">download the Xcode project</a> and give it a go.</p>
<p>Let&#8217;s go with that for today. In the next post I&#8217;ll talk further about this simple example, including instantiation of classes, sending messages to methods and freeing memory of the instance variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-defining-a-class.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

