<?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>[iPhone developer:tips]; &#187; Cocoa</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/cocoa/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 11 Mar 2010 14:32:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Get Application Icon Name</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/get-application-icon-name.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/get-application-icon-name.html#comments</comments>
		<pubDate>Thu, 18 Feb 2010 03:35:31 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5511</guid>
		<description><![CDATA[In a previous post, Get Application Name, I wrote a line of code to get the application name from the app bundle.
The line of code below is a slight modification that shows how to get the name that will appear on the iPhone below the icon:

&#91;&#91;&#91;NSBundle mainBundle&#93; infoDictionary&#93; objectForKey:@&#34;CFBundleDisplayName&#34;&#93;;

The CFBundleDisplayName value corresponds to the Bundle [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post, <a  target="_blank"  href="http://iphonedevelopertips.com/cocoa/get-application-name.html">Get Application Name</a>, I wrote a line of code to get the application name from the app bundle.</p>
<p>The line of code below is a slight modification that shows how to get the name that will appear on the iPhone below the icon:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> infoDictionary<span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CFBundleDisplayName&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The <strong>CFBundleDisplayName</strong> value corresponds to the Bundle Display Name in the application plist file, see the figure below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/icon2.png" alt="" title="icon2" width="490" height="239" class="alignnone size-full wp-image-5514" /></p>
<p>You can see the application icon for this example in this figure:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/icon3.png" alt="" title="icon3" width="329" height="130" class="alignnone size-full wp-image-5521" /></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/get-application-icon-name.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSRange and NSString Objects</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html#comments</comments>
		<pubDate>Thu, 24 Dec 2009 17:52:12 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5132</guid>
		<description><![CDATA[When poking around NSString methods you&#8217;ll find many references to NSRange, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and [...]]]></description>
			<content:encoded><![CDATA[<p>When poking around <strong>NSString</strong> methods you&#8217;ll find many references to <strong>NSRange</strong>, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and number of elements needed (examples to follow). </p>
<h5>NSRange Definition</h5>
<p>NSRange is a structure defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> _NSRange 
<span style="color: #002200;">&#123;</span>
  NSUInteger location;
  NSUInteger length;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">NSRange</span>;</pre></div></div>

<p><span id="more-5132"></span></p>
<p><strong>location</strong> is the starting index in the range (zero based) and <strong>length</strong> is the number of entries in the range. <strong>NSUInteger</strong> is simply an unsigned value that supports both 32 and 64 bit systems. Here is how <strong>NSUInteger</strong> is defined:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">long</span> NSUInteger;
<span style="color: #6e371a;">#else</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> NSUInteger;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<h5>NSRange and Strings</h5>
<p>The example below shows one approach for creating a range and using the same to extract a substring &#8211; the output from below is <strong><em>IPA</em></strong></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>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Starting at position 25, get 3 characters</span>
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> NSMakeRange <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">25</span>, <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// This would also work:</span>
<span style="color: #11740a; font-style: italic;">// NSRange range = {25, 3};</span>
&nbsp;
NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer shortname: %@&quot;</span>, <span style="color: #002200;">&#91;</span>homebrew substringWithRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>If you want to search for a substring, you could write something like the following:</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>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>homebrew rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Did we find the string &quot;IPA&quot; ?</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>range.length &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Range is: %@&quot;</span>, NSStringFromRange<span style="color: #002200;">&#40;</span>range<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from above will display: <strong><em>Range is: {25, 3}</em></strong>. Notice the call to <strong>NSStringFromRange()</strong> which will display the return value (a range) as an <strong>NSString</strong>. There is also a function to create a range from a string: <strong>NSRangeFromString()</strong>.</p>
<p>Let&#8217;s look at one more example, the code below will search for the string &#8220;ia&#8221; starting at the end of the string moving towards the beginning:</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>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Search for the &quot;ia&quot; starting at the end of string</span>
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>homebrew rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ia&quot;</span> options<span style="color: #002200;">:</span>NSBackwardsSearch<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// What did we find</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>range.length &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Range is: %@&quot;</span>, NSStringFromRange<span style="color: #002200;">&#40;</span>range<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The result from above is: <strong><em>Range is: {12, 2}</em></strong> (the &#8220;ia&#8221; inside the word &#8220;India&#8221;).</p>
<h5>NSRange Functions</h5>
<p>Here is a list of functions that work with ranges:</p>
<p>NSEqualRanges()<br />
NSIntersectionRange()<br />
NSLocationInRange()<br />
NSMakeRange()<br />
NSMaxRange()<br />
NSRangeFromString()<br />
NSStringFromRange()<br />
NSUnionRange()</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Compare NSString Objects (Updated)</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/compare-nsstrings-objects.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/compare-nsstrings-objects.html#comments</comments>
		<pubDate>Mon, 21 Dec 2009 16:21:34 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4974</guid>
		<description><![CDATA[This tip is for those new to Objective-C and Cocoa and walks through some basics on comparing NSString objects for equality.
Compare NSString objects with == 

NSString *str1 = @&#34;Homebrew&#34;;
NSString *str2 = @&#34;Homebrew&#34;;
&#160;
// This compares the addresses of the string
if &#40;str1 == str2&#41;
  NSLog &#40;@&#34;str1 equals str2&#34;&#41;;
else 
  NSLog &#40;@&#34;str1 does not equal str2&#34;&#41;;


Although [...]]]></description>
			<content:encoded><![CDATA[<p>This tip is for those new to Objective-C and Cocoa and walks through some basics on comparing NSString objects for equality.</p>
<h5>Compare NSString objects with == </h5>

<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>str1 <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homebrew&quot;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str2 <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homebrew&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// This compares the addresses of the string</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>str1 <span style="color: #002200;">==</span> str2<span style="color: #002200;">&#41;</span>
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str1 equals str2&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span> 
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str1 does not equal str2&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><span id="more-4974"></span><br />
Although this seems to give the results we are after (see the figure below showing Xcode console output), the reason this works is that the compiler can manage strings internally when you define them using the shortcut method (@&#8221;stringhere&#8221;) and will store only one reference internally to duplicates. You can verify that the strings refer to the same content by looking at the locations in memory where the variables are stored:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str1 address in memory: %p&quot;</span>, str1<span style="color: #002200;">&#41;</span>;
NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str2 address in memory: %p&quot;</span>, str2<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>As shown in the figure below, notice the addresses are the same for both strings.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/compare1.png" /></p>
<p>Let&#8217;s modify the above code to compare two more strings, this time with one of the strings being created from a C string:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create a C string</span>
<span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>cStr <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;Homebrew&quot;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str3 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithUTF8String<span style="color: #002200;">:</span>cStr<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str4 <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homebrew&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Wrong - this compares the address of the string</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>str3 <span style="color: #002200;">==</span> str4<span style="color: #002200;">&#41;</span>
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str3 equals str4&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str3 does not equal str4&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Looking at the memory addresses we can do a quick sanity check, notice in the figure below that the addresses are not equal, even though the string &#8220;Homebrew&#8221; is the same for each variable:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str3 address in memory: %p&quot;</span>, str3<span style="color: #002200;">&#41;</span>;
NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str4 address in memory: %p&quot;</span>, str4<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/compare2.png" /></p>
<h5>Compare NSString Objects with isEqualToString</h5>
<p>The right way to go about this is use the <strong>isEqualToString</strong>: method in the <strong>NSString</strong> class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>cStr <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;Homebrew&quot;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str3 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithUTF8String<span style="color: #002200;">:</span>cStr<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str4 <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homebrew&quot;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>str3 isEqualToString<span style="color: #002200;">:</span>str4<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str3 equals str4&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span> 
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str3 does not equal str4&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/compare3.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/compare-nsstrings-objects.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Download, Create and Display an Image from URL</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html#comments</comments>
		<pubDate>Wed, 04 Nov 2009 08:07:19 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[uiimage]]></category>
		<category><![CDATA[uiimageview]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4476</guid>
		<description><![CDATA[This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle. 
URL to Remote Image
We start by creating a URL to the remote resource:

NSURL *url = &#91;NSURL [...]]]></description>
			<content:encoded><![CDATA[<p>This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle. </p>
<h5>URL to Remote Image</h5>
<p>We start by creating a URL to the remote resource:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> 
   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Create UIImage from NSData</h5>
<p>The next step is to build a <strong>UIImage</strong> using the data downloaded from the URL, which consists of an <strong>NSData</strong> object that holds the remote image contents:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Putting it Together</h5>
<p>Here&#8217;s how to wrap it all together, adding the remote image as a subview to an existing view by creating a <strong>UIImageView</strong> from the above <strong>UIImage</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> 
   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png&quot;</span><span style="color: #002200;">&#93;</span>;
UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; 
<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>image<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Date Formatter Examples &#8211; Take 4: Setting Locale</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples-take-4-setting-locale.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples-take-4-setting-locale.html#comments</comments>
		<pubDate>Tue, 07 Jul 2009 13:20:11 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Date and Time]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2531</guid>
		<description><![CDATA[A reader recently wrote and asked how to show a date output in a different language. By default, the NSDateFormatter will use the locale set on the device, so no code specific locale changes are required if you want your app to display in the current locale. 
However, if you need to display a date [...]]]></description>
			<content:encoded><![CDATA[<p>A reader recently wrote and asked how to show a date output in a different language. By default, the NSDateFormatter will use the locale set on the device, so no code specific locale changes are required if you want your app to display in the current locale. </p>
<p>However, if you need to display a date in a locale other than the current setting on the device, here&#8217;s how to do it:<br />
<span id="more-2531"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create date object  </span>
<span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>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>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create date formatter</span>
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEEE MMMM d, YYYY&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set the locale as needed in the formatter (this example uses Japanese)</span>
<span style="color: #002200;">&#91;</span>dateFormat setLocale<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ja_JP&quot;</span><span style="color: #002200;">&#93;</span>
    autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create date string from formatter, using the current date</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span>;  
&nbsp;
<span style="color: #11740a; font-style: italic;">// We're done with this now</span>
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show that the locale on the device is still set to US </span>
<span style="color: #400080;">NSLocale</span> <span style="color: #002200;">*</span>locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> currentLocale<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Current Locale: %@&quot;</span>, <span style="color: #002200;">&#91;</span>locale localeIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Display the date string in format we set above</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Date: %@:&quot;</span>, dateString<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from the code above looks as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/07/locale.png" /></p>
<p><strong>More on Date Formatting:</strong></p>
<p><a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples.html">Date Formatter Examples &#8211; Take 1: NSDateFormatter</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-2.html">Date Formatter Examples &#8211; Take 2: Format Strings</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-3.html">Date Formatter Examples &#8211; Take 3: Date from String</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples-take-4-setting-locale.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone SDK 3 and Deprecated Method Warnings</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/iphone-sdk-3-and-deprecated-methods.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/iphone-sdk-3-and-deprecated-methods.html#comments</comments>
		<pubDate>Tue, 30 Jun 2009 01:55:50 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2499</guid>
		<description><![CDATA[A side-effect of developing code on platforms that continue to evolve, is dealing with methods that become deprecated from one release to another. If you&#8217;ve spent anytime at all with Java, you are all too familiar with this concept. With the release of the iPhone SDK version 3.0, a number of methods have been become [...]]]></description>
			<content:encoded><![CDATA[<p>A side-effect of developing code on platforms that continue to evolve, is dealing with methods that become deprecated from one release to another. If you&#8217;ve spent anytime at all with Java, you are all too familiar with this concept. With the release of the iPhone SDK version 3.0, a number of methods have been become subject to the same fate. </p>
<p>Here is how I came about this in my most recent app. I have a subclass of <strong>UITableViewCell</strong> that looks as follows:<br />
<span id="more-2499"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> WeaponCell <span style="color: #002200;">:</span> UITableViewCell 
<span style="color: #002200;">&#123;</span>
  UIImageView <span style="color: #002200;">*</span>image; 
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
...
&nbsp;
<span style="color: #11740a; font-style: italic;">// An instance of WeaponCell</span>
WeaponCell <span style="color: #002200;">*</span>cell;</pre></div></div>

<p>Prior to installing the 3.0 SDK, I would set the image as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  cell.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;weapon.png&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>However, with 3.0, the <strong>image</strong> method for <strong>UITableViewCell</strong> now states this: &#8220;Deprecated. Instead use the <strong>imageView</strong> property to get <strong>UIImageView</strong> object and then get or set the encapsulated image.&#8221;</p>
<p>Trouble is, you can&#8217;t simply make the appropriate code change if there&#8217;s any chance you need to build for a device that is running 2.x (or earlier). For example, I had to create an adhoc release today for someone who is running a device that has not been upgraded, and rolling back the code to pre-3.0 was not on the agenda. Here&#8217;s a much better solution:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// That's two underscores before IPHONE</span>
<span style="color: #6e371a;">#if __IPHONE_3_0</span>
  cell.imageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;weapon.png&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#else</span>
  cell.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;weapon.png&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>Likewise, the font property of <strong>UIButton</strong> has been deprecated and you now are directed to use <strong>titleLabel</strong> property instead.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIButton <span style="color: #002200;">*</span>backButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#if __IPHONE_3_0</span>
  backButton.titleLabel.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#else</span>
  backButton.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>Deprecated methods, one of those things that you get to live with as a software developer. Good news is, there are reasonable work-arounds that make for managing this issue pretty painless.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/iphone-sdk-3-and-deprecated-methods.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>UIColor Macros</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/uicolor-macros.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/uicolor-macros.html#comments</comments>
		<pubDate>Fri, 05 Jun 2009 12:23:16 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2342</guid>
		<description><![CDATA[Below are two macros I paste inside every new iPhone project. Besides saving a few keystrokes, they work well when using the color picker application. Let&#8217;s look at the macros first:

 #define RGB(r, g, b) 
    &#91;UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1&#93;
 #define RGBA(r, g, b, a) 
    &#91;UIColor colorWithRed:r/255.0 [...]]]></description>
			<content:encoded><![CDATA[<p>Below are two macros I paste inside every new iPhone project. Besides saving a few keystrokes, they work well when using the color picker application. Let&#8217;s look at the macros first:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #6e371a;">#define RGB(r, g, b) </span>
    <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span>r<span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> green<span style="color: #002200;">:</span>g<span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> blue<span style="color: #002200;">:</span>b<span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>
 <span style="color: #6e371a;">#define RGBA(r, g, b, a) </span>
    <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span>r<span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> green<span style="color: #002200;">:</span>g<span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> blue<span style="color: #002200;">:</span>b<span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> alpha<span style="color: #002200;">:</span>a<span style="color: #002200;">&#93;</span></pre></div></div>

<p>Here&#8217;s the code without/with the macro:<br />
<span id="more-2342"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #11740a; font-style: italic;">// Code without the macro</span>
  msgLabel.textColor <span style="color: #002200;">=</span> 
     <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span><span style="color: #2400d9;">255</span><span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> green<span style="color: #002200;">:</span><span style="color: #2400d9;">251</span><span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> blue<span style="color: #002200;">:</span><span style="color: #2400d9;">204</span><span style="color: #002200;">/</span><span style="color: #2400d9;">255.0</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Or like this...</span>
  msgLabel.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span> green<span style="color: #002200;">:</span>.98 blue<span style="color: #002200;">:</span>.8 alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
 <span style="color: #11740a; font-style: italic;">// Code with macro</span>
  msgLabel.textColor <span style="color: #002200;">=</span> RGB<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">255</span>, <span style="color: #2400d9;">251</span>, <span style="color: #2400d9;">204</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>UIColor expects its parameters to be expressed as values in the range of 0.0 to 1.0, so I pass in the color value I&#8217;m looking for between 0 and 255, and let the macro do the math for me.</p>
<p>Let me show you how I use the macro with the color picker application. The figure below shows a screenshot of the color picker &#8211; at this point I have selected the color I am interested in, so I choose rgb from the dropdown menu.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/06/color2.png" /></p>
<p>From here, I copy the value from the field <strong>&#8220;Hex:&#8221;</strong>, paste this into my project and finish by changing <strong>rgb</strong> to <strong>RGB</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/uicolor-macros.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Changing Views to Landscape Mode</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/changing-views-to-landscape-mode.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/changing-views-to-landscape-mode.html#comments</comments>
		<pubDate>Tue, 14 Apr 2009 03:17:03 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2069</guid>
		<description><![CDATA[As you well know, by default, iPhone applications launch in portrait mode. If you need to start your application in landscape mode, you can set UIInterfaceOrientation key in the plist file. The two possible values for this key are: UIInterfaceOrientationLandscapeLeft (iPhone home button will be on the left) and UIInterfaceOrientationLandscapeRight (home button on right).

For views [...]]]></description>
			<content:encoded><![CDATA[<p>As you well know, by default, iPhone applications launch in portrait mode. If you need to start your application in landscape mode, you can set <strong>UIInterfaceOrientation</strong> key in the plist file. The two possible values for this key are: <strong>UIInterfaceOrientationLandscapeLeft</strong> (iPhone home button will be on the left) and <strong>UIInterfaceOrientationLandscapeRight</strong> (home button on right).<br />
<span id="more-2069"></span></p>
<p>For views to be shown in landscape mode, you must manually rotate the coordinate system 90 degrees. Here&#8217;s one way you can do that, let&#8217;s assume we have a class that is of type <strong>UIViewController</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SomeViewController <span style="color: #002200;">:</span> UIViewController 
<span style="color: #002200;">&#123;</span>
...
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Inside the implementation file of <strong>SomeViewController</strong>, here&#8217;s one approach you can use within the <strong>viewDidLoad</strong> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td 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>viewDidLoad
<span style="color: #002200;">&#123;</span>
  self.view.transform <span style="color: #002200;">=</span> CGAffineTransformIdentity;
  self.view.transform <span style="color: #002200;">=</span> CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>M_PI <span style="color: #002200;">*</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">90</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">180.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; 
  self.view.bounds <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Specifying <strong>CGAffineTransformMakeRotation</strong> call with 90 degrees as shown above will show the view in the <strong>UIInterfaceOrientationLandscapeRight</strong>. Setting this value to -90 will show the view in the <strong>UIInterfaceOrientationLandscapeLeft</strong>.</p>
<p>If you set the orientation in the plist file, you&#8217;ll need to coordinate the value set in the <strong>loadView</strong> method so the status bar appears as expected, that is, across the top of the display. If you choose set the orientation in the plist as <strong>UIInterfaceOrientationLandscapeLeft</strong>, use -90 as the rotation value, set to 90 for <strong>UIInterfaceOrientationLandscapeRight</strong>.</p>
<p>There are more ways than one to rotate views to landscape mode, if you have a preferred means, please post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/changing-views-to-landscape-mode.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introduction to Protocols</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/introduction-to-protocols.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/introduction-to-protocols.html#comments</comments>
		<pubDate>Mon, 30 Mar 2009 13:03:41 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1988</guid>
		<description><![CDATA[What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts. </p>
<p>Cocoa uses protocols to support interprocess communication through Objective-C messages. In addition, since Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.<br />
<span id="more-1988"></span></p>
<p>A good example of a protocol is <em>NSCoding</em>, which has two required methods that a class must implement. This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> <span style="color: #2a6f76;">NSCoding</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>encodeWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aCoder;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aDecoder;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>To adopt a protocol, enclose the name of the protocol in <> as below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Interface</span>
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;NSCoding&gt; 
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Implementation</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>encodeWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aCoder
<span style="color: #002200;">&#123;</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>initWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aDecoder
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Defining a Protocol</strong></p>
<p>You can create both required an optional methods within a protocol. What follows is a definion of a protocol named &#8216;Fubar&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> Fubar
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>send<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>data;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>receive;
@optional
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>status;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>To use the protocol, as with the example above, specify the protocol in the interface and write the required methods in the class implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Interface</span>
<span style="color: #a61390;">@interface</span> AnotherClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;Fubar&gt; 
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Implementation</span>
<span style="color: #a61390;">@implementation</span> AnotherClass
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>send<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>data
<span style="color: #002200;">&#123;</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>receive
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Optional methods</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>status
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>If you are from a Java background, protocols should look familiar as they are analogous to an interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/introduction-to-protocols.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Basics of Notifications</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/basics-of-notifications.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/basics-of-notifications.html#comments</comments>
		<pubDate>Thu, 26 Mar 2009 02:46:59 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1852</guid>
		<description><![CDATA[What follows is a brief guide to working with Notifications in Cocoa. I&#8217;ll cover the basics, including registering an observer and posting notifications,  just enough to start using notifications in your iPhone apps.
There is an instance of NSNotificationCenter available to every running iPhone application. This class acts as an intermediary to facilitate communication between [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a brief guide to working with Notifications in Cocoa. I&#8217;ll cover the basics, including registering an observer and posting notifications,  just enough to start using notifications in your iPhone apps.</p>
<p>There is an instance of <em>NSNotificationCenter</em> available to every running iPhone application. This class acts as an intermediary to facilitate communication between objects that are interested in being notified at some point in the future (these objects are known as the  <em>observers</em>) and a <em>poster</em> that posts to the notification center, resulting in all observers (registered for a specific notification) being called.<br />
<span id="more-1852"></span></p>
<p>To give you an idea of where you might use notifications, consider how you might handle downloading of data in a background thread. I recently used notifications in this scenario as I wanted to be notified when a web-service call completed. Upon receiving a notification, I then proceeded to populate a view with the data retrieved, or with an error message if the data access failed. </p>
<p>The 30,000 foot view consists of two steps:</p>
<p><strong>Step #1</strong><br />
Register an observer, which requests that a selector to be called when a specific notification is posted.</p>
<p><strong>Step #2</strong><br />
Post a notification, which will result in all registered observers being called.</p>
<p>For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Register observer to be notified when download of data is complete</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                                   selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>downloadDataComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                                   name<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This code registers an observer for a notification with the name <em>NOTIF_DataComplete</em> (more on that below). When a notification is posted with the same name, the method <em>downloadDataComplete</em> will be called. </p>
<p>Posting a notification with the name <em>NOTIF_DataComplete</em> would look as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Post notification that the download is complete  </span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> 
                    postNotificationName<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Let&#8217;s show how this might like in a working example.</p>
<p><strong>Class Registering as Observer</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  SomeClass.h</span>
&nbsp;
<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>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// For name of notification</span>
<span style="color: #a61390;">extern</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> NOTIF_DataComplete;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Below is the code for the implementation of <em>SomeClass</em>. Notice on line 6 the name of the notification is defined. On line 40 we register as an observer for the named notification, specifying the selector <em>@selector(downloadDataComplete:)</em>. Once the notification is posted, the method on line 26 is called.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  SomeClass.m</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Name of notification</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> NOTIF_DataComplete <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DataComplete&quot;</span>;
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Private Interface</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">&#40;</span>private<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dataDownloadComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Private Methods</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Notifications of data downloads 
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>downloadDataComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif 
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received Notification - Data has been downloaded&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Initialization</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Initialization
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Other initialization code here...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Register observer to be called when download of data is complete</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                         selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>downloadDataComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                         name<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Cleanup</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Cleanup 
*--------------------------------------------------------------------------*/</span> 
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> removeObserver<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>Class That Posts Notification</strong></p>
<p>To keep things simple, I&#8217;ll add a few lines to the <em>applicationDidFinishLaunching</em> method to show you how posting a notification might look:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td 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>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
  SomeClass <span style="color: #002200;">*</span>someclass <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;">// ...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Post a notification that the data has been downloaded  </span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> 
            postNotificationName<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I create an instance of SomeClass and follow this by posting of a notification of the name <em>NOTIF_DataComplete</em>. This example is a little contrived, however, I&#8217;m sure you get the idea. Although a short example, it shows just how easy it is to register an observer and post notifications.</p>
<p>There are various other nuances to working with notifications that can be quite helpful when the time comes that you need more robust support for communication among objects. For example, you could specify that you are interested in receiving a notification by name (as above), however, only if that notification has a specific object affiliated with it. This would allow you to use the same notification name, however, only notify certain objects. One use of this could be if you have multiple downloads and want to use notifications to be notify a specific object that it&#8217;s download is complete.</p>
<p>If you are working with multiple threads, which would be the case if you need to fire off multiple downloads in the background, you can look into distributed notifications, which allow notifications to be delivered to a particular thread. For more information, you can read  <em>Introduction to Notification Programming Topics</em> available in the iPhone Reference Library.</p>
<p><strong>Download: </strong> <a href="http://iphonedevelopertips.com/wp-content/uploads/2009/03/notifs.zip">Xcode project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/basics-of-notifications.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
