<?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>ObjColumnist &#187; Spencer MacDonald</title>
	<atom:link href="http://objcolumnist.com/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://objcolumnist.com</link>
	<description>Coding under the Hammer</description>
	<lastBuildDate>Tue, 10 Jan 2012 20:27:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Shipping a 1.0 is Hard</title>
		<link>http://objcolumnist.com/2011/12/19/shipping-a-1-0-is-hard/</link>
		<comments>http://objcolumnist.com/2011/12/19/shipping-a-1-0-is-hard/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 11:12:01 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=584</guid>
		<description><![CDATA[The first version of an application is different to any other, and it is the hardest one to actually ship. This is especially true if you are an indie developer. When you start a new application you probably have a list of features and a sketch of what you think/hope/wish the 1.0 version of your [...]]]></description>
			<content:encoded><![CDATA[<p>The first version of an application is different to any other, and it is the hardest one to actually ship. This is especially true if you are an indie developer.</p>
<p>When you start a new application you probably have a list of features and a sketch of what you think/hope/wish the 1.0 version of your application will look like. As an indie developer you are the only one that knows exactly what is on this list, but you often feel like you can&#8217;t release an application until the whole list is completed, even though nobody else would be any the wiser.</p>
<p>The other major difference about a 1.0 version of an application is the lack of immediate pressure to release it. When you have released an application you are often pressured into releasing an update for a new feature or simply to fix some bugs. As an indie developer the only pressure you get is from yourself (and I try not to moan at me too often).</p>
<p>So over a week ago I finally decided my baby was ready to see the world, so I sent off the 1.0 version of my application <a href="http://www.actionify.com">Actionify</a> to Apple and waited for it to go through the review process. To my pleasant surprise there was no problems first time round (Apple usually find something) and Actionify was released on Friday. <a href="http://www.actionify.com">Actionify</a> is a GTD inspired Task Manager, that also offers a cloud sync subscription that allows users to collaborate on Projects. If you want to know more about it, you can click on the <a href="http://www.actionify.com">link</a> here, but I won&#8217;t overload this post info. I am very pleased with how Actionify has turned out, the 1.0 misses a few features from my original list but also some additions that I added due to the feedback I received during the beta testing (thank you testers!!!). Inevitable it took longer than I had originally hoped, but this was mainly down to me underestimating the amount of effort and paperwork it took to set up a limited company (in the UK) and everything that goes with that (e.g. Banking, Transferring my iTunes Connect Account etc etc). In terms of development time the project probably only over run by 1 or 2 months, while this is not ideal, it isn&#8217;t to bad either.</p>
<p>In terms of technology, Actionify requires Mac OS 10.7 as the UI is mainly built with view based table views and the new Core Data APIs. I think view based table views shaved about 2 months off of my development time, so support for 10.6 wasn&#8217;t really an option for me. 10.7 also has a JSON Parser (<a href="http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html"><strong>NSJSONSerialization</strong></a>) and Popovers (<a href="http://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSPopover_Class/Reference/Reference.html"><strong>NSPopover</strong></a>) built in, and although there are open source projects that offer similar functionally, I prefer to only depend on code by Apple and myself (rightly or wrongly) wherever possible.</p>
<p>The application syncs with a <a href="http://rubyonrails.org/">Rails</a> application that I host on <a href="http://www.heroku.com/">Heroku</a>, and I couldn&#8217;t recommend Rails and Heroku enough. Rails is a great framework and Ruby is a great programming language, the best thing about Rails (for a non web developer) is everything has its place. Rails forces you to have a certain folder structure and I found this extremely beneficial … you can also add features with only a few lines of code which can only be a good thing. Heroku&#8217;s main benefit is you don&#8217;t have to think about servers and you just have to worry about your app. You simply deploy your code using a git push and you&#8217;re done. Moreover the majority of basic Heroku Add Ons are free, so you can start using Heroku without any risk (I am honestly not on commission, I just like it <img src='http://objcolumnist.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<p>I want to end this post by saying no matter how many times you release an application, seeing other people downloading it and using it is always the best feeling a developer can get, so if you can … SHIP IT!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/12/19/shipping-a-1-0-is-hard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keeping the Static Analyzer Happy: Prefixed Initializers</title>
		<link>http://objcolumnist.com/2011/11/03/keeping-the-static-analyzer-happy-prefixed-initializers/</link>
		<comments>http://objcolumnist.com/2011/11/03/keeping-the-static-analyzer-happy-prefixed-initializers/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 10:12:55 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=566</guid>
		<description><![CDATA[The latest version of Xcode ships with LLVM 3.0 as it&#8217;s default compiler, and one of the first things that you will notice is that is a lot more thorough when it analyses your code compared to previous versions (which can only be a good thing). One thing that the static analyser now warns you [...]]]></description>
			<content:encoded><![CDATA[<p>The latest version of Xcode ships with LLVM 3.0 as it&#8217;s default compiler, and one of the first things that you will notice is that is a lot more thorough when it analyses your code compared to previous versions (which can only be a good thing). One thing that the static analyser now warns you about, is that you are over releasing objects that are returned from prefixed intalizer methods (init), such as in a category (for my previous posts on categories see <a href="http://objcolumnist.com/2009/05/22/categories/">here</a> and  <a href="http://objcolumnist.com/2011/09/08/adding-block-support-to-existing-classes-without-subclassing/">here</a>).</p>
<p>For example my <em>NSString</em> category has the following 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;">id</span><span style="color: #002200;">&#41;</span>MCSM_initWithComponents<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>components 
seperatedByString<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>seperator;</pre></div></div>

<p>And it&#8217;s implementation looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>MCSM_initWithComponents<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>components 
seperatedByString<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>seperator<span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>componentizedString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> <span style="color: #a61390;">string</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    NSUInteger i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>component <span style="color: #a61390;">in</span> components<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
&nbsp;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>i <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>componentizedString appendString<span style="color: #002200;">:</span>component<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span><span style="color: #a61390;">else</span><span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>componentizedString appendFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@%@&quot;</span>,seperator,component<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
        i<span style="color: #002200;">++</span>;
&nbsp;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self initWithString<span style="color: #002200;">:</span>componentizedString<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This method takes an array of strings, and joins together using the separator parameter and can be used in the following way:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>components <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;two&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; 
<span style="color: #a61390;">string</span> <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> MCSM_initWithComponents<span style="color: #002200;">:</span>components seperatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;,&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;%@&quot;</span>,<span style="color: #a61390;">string</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This would output:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">one,two</pre></div></div>

<p>As this method is in a category of <em>NSString</em> I don&#8217;t want it to clash with any other implementations. The common practise in Objective-C is to prefix categories methods (due to the lack of namespaces), so I have with <em>MCSM_</em>. The issue is that the static analyser will now think that this method returns an autoreleased object, as the method does not begin with init, new, copy or alloc. This means when you release the object the static analyser will complain about you over releasing an object.</p>
<p>So how do you fix this?</p>
<p>To fix this you can tell the compiler that the method returns a retained object by using the source annotation <em>NS_RETURNS_RETAINED</em>, which means your interface would look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>MCSM_initWithComponents<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>components 
seperatedByString<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>seperator NS_RETURNS_RETAINED;</pre></div></div>

<p>Instead of <em>NS_RETURNS_RETAINED</em> you can also use <em>__attribute__((ns_returns_retained))</em>, which is a longer way of writing the same thing:</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>MCSM_initWithComponents<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>components 
seperatedByString<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>seperator __attribute__<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>ns_returns_retained<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So thats all fixed? Unfortunately not quite yet. The static analyser will now complain about a memory leak, as we have allocated a <em>NSString</em> by doing <em>[NSString alloc]</em>, but then it isn&#8217;t referenced again in our code. For a method that begins with init, the static analyser knows that the method consumes the variable (which means it releases the parameter upon completion), and that is the behaviour we need. </p>
<p>To do this we have to use the source annotation <em>__attribute__((ns_consumes_self))</em> in conjunction <em>__attribute__((ns_returns_retained))</em>, which means your interface will look like:</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>MCSM_initWithComponents<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>components 
seperatedByString<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>seperator __attribute__<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>ns_consumes_self<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>__attribute__<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>ns_returns_retained<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>And that will fix it.</p>
<p><strong>Summary:</strong></p>
<p>You don&#8217;t need to use the source annotations if your code obeys the Objective-C naming conventions, but in certain circumstances like the one above you need to help the Static Analyser do its job. As LLVM forms the the backbone of Automatic Reference Counting (ARC), you still need to do this even if your not retaining and releasing memory yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/11/03/keeping-the-static-analyzer-happy-prefixed-initializers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Steve Jobs 1955-2011</title>
		<link>http://objcolumnist.com/2011/10/06/steve-jobs-1955-2011/</link>
		<comments>http://objcolumnist.com/2011/10/06/steve-jobs-1955-2011/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 09:17:49 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=559</guid>
		<description><![CDATA[Having never met the man, it does feel strange that I am so deeply sadden by Steve Jobs passing, and I suppose that is because what I do today is really down to him. Not only did Steve Jobs have the vision to create the Mac, iPhone and iPad that are in front of me [...]]]></description>
			<content:encoded><![CDATA[<p>Having never met the man, it does feel strange that I am so deeply sadden by Steve Jobs passing, and I suppose that is because what I do today is really down to him. Not only did Steve Jobs have the vision to create the Mac, iPhone and iPad that are in front of me on my desk, he also had the passion to make me want them too.</p>
<p>Looked up to by millions, Steve Jobs was a charismatic visionary, who will go down as one of the greatest, if not the greatest CEO of all time. Not only did he start the computing revolution when he founded Apple in 70s, in his second stint as Apple CEO he brought a company on the brink of bankruptcy to be the most valuable company in the world in just under 15 years. Time and time again he ripped up the rule book and released product after product that change the world for good.</p>
<p>I could go on about how he changed the world but you already know that, and there are plenty of other articles that put it better than me. What I would like to say is that Steve Jobs&#8217; passing has reiterated 3 things to me:</p>
<p><strong>1) Find what you love</strong></p>
<blockquote><p>
You&#8217;ve got to find what you love. And that is as true for your work as it is for your lovers.</p>
<p>Steve Jobs, Standford Speech (2005)
</p></blockquote>
<p>Don&#8217;t settle for second best, this is as true as it is for work as it is for love. You need to find what you love doing, and do that. If it makes you millions then thats great, but if it makes you happy then thats what counts.</p>
<p><strong>2) Work hard to make it simple</strong></p>
<blockquote><p>That&#8217;s been one of my mantras &#8211; focus and simplicity. Simple can be harder than complex: you have to work hard to get your thinking clean to make it simple. But it&#8217;s worth it in the end because once you get there, you can move mountains.</p>
<p>Steve Jobs, Interview with Business Week, 1998
</p></blockquote>
<p>If you design an make stuff like me, your appreciate that one of the things that Steve Jobs has proven, is that users are willing to pay more for simplicity.</p>
<p>My favourite Steve Jobs Quote is:</p>
<blockquote><p>Design is not just what it looks like and feels like. Design is how it works.
</p></blockquote>
<p>This goes hand in hand with simplify, it has to appear simple to use and be simple to use. Attention to detail in every aspect of design is key to a successful product. Make this your Mantra.</p>
<p><strong>3) All good thing must come to an end</strong></p>
<blockquote><p>No one wants to die. Even people who want to go to heaven don&#8217;t want to die to get there. And yet death is the destination we all share. No one has ever escaped it.</p>
<p>Steve Jobs, Standford Speech (2005)</p></blockquote>
<p>No matter how visionary you are, no matter how much money you have in the bank, one day you will be gone. Not everyone like Steve Jobs has the chance to become a legend, but you do have the chance to leave a legacy. </p>
<p>Stay Hungry. Stay Foolish.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/10/06/steve-jobs-1955-2011/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Let&#8217;s Talk iPhone Keynote Predictions</title>
		<link>http://objcolumnist.com/2011/10/03/lets-talk-iphone-keynote-predictions/</link>
		<comments>http://objcolumnist.com/2011/10/03/lets-talk-iphone-keynote-predictions/#comments</comments>
		<pubDate>Mon, 03 Oct 2011 15:45:34 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Keynote]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=549</guid>
		<description><![CDATA[So tomorrow is the day that we finally get to see the successor to the phenomenally successful iPhone 4, but what is going to be new? 1) iOS 5 The one thing that the new iPhone is guaranteed to will ship with iOS 5. If you have not already seen the WWDC Keynote (if not [...]]]></description>
			<content:encoded><![CDATA[<p>So tomorrow is the day that we finally get to see the successor to the phenomenally successful iPhone 4, but what is going to be new?</p>
<p><strong>1) iOS 5</strong><br />
The one thing that the new iPhone is guaranteed to will ship with iOS 5. If you have not already seen the WWDC Keynote (if not why not?) then you already know whats coming in iOS 5. As a reminder you can look on <a href="http://www.apple.com/ios/ios5/features.html">Apple&#8217;s Website</a>. The biggest change is how notifications work, they are less obtrusive and are all visible in one place. The other change that you will notice straight away (if you also have friends running iOS 5) is iMessage. iMessage is Apple&#8217;s awnser to RIM&#8217;s Blackberry messagener. It allows you to send free message to other user&#8217;s iOS devices, and you also get the option of recieving sent, received and read receipts.</p>
<p><strong>2) iCloud</strong></p>
<p>Although strictly speaking part of iOS 5, iCloud is a headline feature all of it&#8217;s own. It allows applications to store data &#8220;in the cloud&#8221; so it is accessible on all of your devices, be it iPhone, iPod Touch, iPad, Mac or one of those PC things. An important thing to note which differentiates iCloud from services like DropBox, is that applications can only see their own data (it is sandboxed by company). So for example, you can&#8217;t edit a photo in one application, and then upload it using another application by finding the edited image on the file system, in fact as a user you can&#8217;t even see your iCloud file system at all!!</p>
<p><strong>3) A5 Chip</strong></p>
<p>The iPhone 4 has an under clocked version of the iPad&#8217;s A4 processor, so it would make sense that new iPhone will have an under clocked version of the iPad 2&#8242;s A5 processor. The A5 processor (on the iPad 2 at least) has two cores, which will make your iPhone experience a little bit more snappy.</p>
<p><strong>4) 8MP Camera</strong></p>
<p>Not confirmed by anyone, but it would make sense (and would also be relatively cheap) to put a higher quality camera in the new iPhone. Bumping up the megapixel count for 5MP to 8MP is a logical step, and a few phones already contain this sensor.</p>
<p><strong>5) 64 GB of Storage</strong></p>
<p>The iPhone has been stuck at 32GB of storage since the arrival of the 3GS, so I expect an upgrade to 64GB so I am able to carry half of music collection, instead of a quarter of it. (I could really do with a 128GB iPhone if you couldn&#8217;t tell)</p>
<p><strong>6) iPhone 5 or 4S</strong></p>
<p>If I was Apple, I would call it the iPhone 5 how ever minor the update is, and that is for 2 reasons:</p>
<ol>
<li>I think people are more likely to upgrade to an iPhone 5, as subconsciously it just sounds like a bigger update</li>
<li>If it is called the iPhone 4S, then what is the next iPhone going to called? The 6th iPhone surely can&#8217;t be called the iPhone 5. (I know there has been a 3GS, but that was because Apple called the 2nd iPhone, the iPhone 3G)</li>
</ol>
<p><strong>Other Updates</strong></p>
<p>In addition to the iPhone, I expect the iPod Touch to get an update so it&#8217;s specifications are inline with the new iPhone&#8217;s. I don&#8217;t think it will get a 8MP camera though, but we might even see a 128GB Model. If we do see a 128GB Model, I do expect the iPod Classic to retire to the gadget museum. If we don&#8217;t see a 128GB, I don&#8217;t expect to see an iPod Classic update anyway &#8230; does anybody still buy them?</p>
<p>The iPod Nano will also get a refresh for the holiday season, now wouldn&#8217;t it be amazing if that run iOS 5&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/10/03/lets-talk-iphone-keynote-predictions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Block Support to Existing Classes (Without Subclassing)</title>
		<link>http://objcolumnist.com/2011/09/08/adding-block-support-to-existing-classes-without-subclassing/</link>
		<comments>http://objcolumnist.com/2011/09/08/adding-block-support-to-existing-classes-without-subclassing/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 08:42:37 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=515</guid>
		<description><![CDATA[Blocks are great aren&#8217;t they? Amongst many other things, they allow you to put all your completion logic right next to where you call an asynchronous method. Apple has added block support for completion handlers in such APIs such as Core Animation, in fact I have already done a post on this previously. But what [...]]]></description>
			<content:encoded><![CDATA[<p>Blocks are great aren&#8217;t they? Amongst many other things, they allow you to put all your completion logic right next to where you call an asynchronous method. Apple has added block support for completion handlers in such APIs such as Core Animation, in fact I have already done a post on this <a href="http://objcolumnist.com/2010/09/19/core-animation-using-blocks/">previously</a>.  But what do you do, if the class you want to use is stuck in the past and still uses the older delegate style approach. This is where you have to write a method to handle all the delegate callbacks for a given class? Well thankfully there is a nice and clean way of adding block support to a class using a category.</p>
<p>I&#8217;ve previously written 2 posts on categories (which you can find <a href="http://objcolumnist.com/2009/05/22/categories/">here</a> and <a href="http://objcolumnist.com/2011/05/03/performing-a-block-of-code-on-a-given-thread/">here</a>), and how they allow you to extend a class&#8217;s functionality by adding additional methods to it without subclassing it. The key word being <strong>methods</strong> (or indeed <strong>selectors</strong> if your getting technical), and not <strong>properties</strong> or <strong>instance variables</strong>. If you need to call a block after an asynchronous action has completed, you&#8217;re going to have to store it somewhere in the meantime, and that is the main focus of this post.</p>
<p>Take <strong>UIAlertView</strong> for example, you need to register for a callback when the user presses a button.</p>
<p>So you&#8217;d often have something like:</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>IBAction<span style="color: #002200;">&#41;</span>showAlert<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender<span style="color: #002200;">&#123;</span>
&nbsp;
UIAlertView <span style="color: #002200;">*</span>alertView <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;YES or NO&quot;</span>
message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Select One&quot;</span> 
delegate<span style="color: #002200;">:</span>self 
cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NO&quot;</span> 
otherButtonTitles<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;YES&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>alertView autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Then you would need to implement a method with the correct signature to handle it:</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>alertView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAlertView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>alertView 
didDismissWithButtonIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>buttonIndex<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>buttonIndex <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//Do Something</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This may look fine in the above example, but in a real iOS application the callback method may have to handle lots of different alert views, and we still have the handler code separated from where we actually setup and show the alert view.</p>
<p>So how can we solve this?</p>
<p>We are going to add a completion handler to <strong>UIAlertView</strong>, which doesn&#8217;t return anything and takes the button index we get given by the <strong>UIAlertViews</strong> delegate method as a parameter. To keep things clean we will define our completion handler in our categories header:</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;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>MCSMUIAlertViewCompletionHandler<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>NSUInteger buttonIndex<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So the first thing you will need to do is set the completion hander block on an alert view and <strong>store</strong> it for when we get the delegate callback. For this we will use the Objective-C runtime function: <strong>objc_setAssociatedObject</strong>.</p>
<p>The <strong>objc_setAssociatedObject</strong> function takes 4 parameters:</p>
<ol>
<li>The object that you want to add the association too, in this case the <strong>UIAlertView</strong></li>
<li>The unique key so you can retrieve the value later</li>
<li>The value you want to associate, which in this case is the completion handler block</li>
<li>The association policy, which tells the runtime wether this association retains, copies or assigns the value.</li>
</ol>
<p>The best way to describe how this function works, is that it treats an object like and <strong>NSDictionary</strong>. It allows you to set a given value for a given key, but unlike <strong>NSDictionary</strong> you can specify if the value is assign, retained or copied.</p>
<p>To use this method you need to include the runtime header:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;objc/runtime.h&gt;</span></pre></div></div>

<p>And we will define our key as a constant to keep things tidy, and so we don&#8217;t make any typos later on:</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> <span style="color: #a61390;">const</span> MCSMUIAlertViewCompletionHandlerKey <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MCSMUIAlertViewCompletionHandlerKey&quot;</span>;</pre></div></div>

<p>Then in another category method on <strong>UIAlertView</strong> all you need to do is set the alert view as its own delegate, and store the block as an associated object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>MCSM_setCompletionHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MCSMUIAlertViewCompletionHandler<span style="color: #002200;">&#41;</span>handler<span style="color: #002200;">&#123;</span>
self.delegate <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>id&lt;UIAlertViewDelegate&gt;<span style="color: #002200;">&#41;</span>self; 
&nbsp;
objc_setAssociatedObject<span style="color: #002200;">&#40;</span>
self, 
MCSMUIAlertViewCompletionHandlerKey,
handler,
OBJC_ASSOCIATION_COPY_NONATOMIC<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As the <strong>UIAlertView</strong> is now it own delegate, it will receive the <strong>alertView:didDismissWithButtonIndex:</strong> callback. In this method you will need to retrieve the completion handler block so you can call it with the button index argument. To do this you will need to use another Objective-C runtime function <strong>objc_getAssociatedObject</strong></p>
<p>The <strong>objc_getAssociatedObject</strong> function takes 2 parameters:</p>
<ol>
<li>The object to retrieve the association from</li>
<li>The unique key for the association</li>
</ol>
<p>This will mean that you end up with a method looking like this:</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>alertView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAlertView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>alertView didDismissWithButtonIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>buttonIndex<span style="color: #002200;">&#123;</span>
&nbsp;
MCSMUIAlertViewCompletionHandler handler <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #11740a; font-style: italic;">//Get the Handler</span>
handler <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>MCSMUIAlertViewCompletionHandler<span style="color: #002200;">&#41;</span>objc_getAssociatedObject<span style="color: #002200;">&#40;</span>
self,
MCSMUIAlertViewCompletionHandlerKey<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//If there is a handler call the handler</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>handler<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  handler<span style="color: #002200;">&#40;</span>buttonIndex<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Release the block by setting the associated object to nil</span>
objc_setAssociatedObject<span style="color: #002200;">&#40;</span>
self, 
MCSMUIAlertViewCompletionHandlerKey, 
<span style="color: #a61390;">nil</span>, 
OBJC_ASSOCIATION_COPY_NONATOMIC<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So now that we have added this category to <strong>UIAlertView</strong>, all we have to do is update the code that creates and shows the alert view:</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>IBAction<span style="color: #002200;">&#41;</span>showAlert<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender<span style="color: #002200;">&#123;</span>
&nbsp;
UIAlertView <span style="color: #002200;">*</span>alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;YES or NO&quot;</span>
message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Select One&quot;</span> 
delegate<span style="color: #002200;">:</span>self 
cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NO&quot;</span> 
otherButtonTitles<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;YES&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>alertView MCSM_setCompletionHandler<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span>NSUInteger buttonIndex<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>buttonIndex <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//Do Something</span>
<span style="color: #002200;">&#125;</span> 
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>alertView autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>You can grab the code for this on Git Hub <a href="https://github.com/ObjColumnist/MCSMUIAlertViewCategory" title="here">here</a> and have a play with it yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/09/08/adding-block-support-to-existing-classes-without-subclassing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mac OS 10.7 Lion &#8211; My Favorite new APIs</title>
		<link>http://objcolumnist.com/2011/07/20/mac-os-10-7-lion-my-favorite-new-apis/</link>
		<comments>http://objcolumnist.com/2011/07/20/mac-os-10-7-lion-my-favorite-new-apis/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 16:46:24 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=476</guid>
		<description><![CDATA[So after months of waiting, Mac 10.7 has finally been released to the general public. Although it has been given some minor spit and polish on the UI front when compared to it&#8217;s predecessor Mac OS 10.6, most of the improvements are under the hood. Thankfully the majority of these changes have been made available [...]]]></description>
			<content:encoded><![CDATA[<p>So after months of waiting, Mac 10.7 has finally been released to the general public. Although it has been given some minor spit and polish on the UI front when compared to it&#8217;s predecessor Mac OS 10.6, most of the improvements are under the hood. Thankfully the majority of these changes have been made available to developers in the form of APIs. With that in mind I thought I would point out some of my favourite new APIs that I have been using in the developer previews.</p>
<h2><strong>NSPopover</strong></h2>
<p><strong>NSPopover</strong> works a lot like <strong>UIPopoverController</strong> on iOS. You give an <strong>NSPopover</strong> an instance of <strong>NSViewController</strong> (or one of your subclasses), and you can then present this view controller in a popover.</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>IBAction<span style="color: #002200;">&#41;</span>showPopoverFromButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//Create the popover</span>
popover <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>NSPopover alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Set the content view controller</span>
popover.contentViewController <span style="color: #002200;">=</span> popoverViewController;
&nbsp;
popover.animates <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//So we get told when the popover has closed</span>
popover.delegate <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>id&lt;NSPopoverDelegate&gt;<span style="color: #002200;">&#41;</span>self;
&nbsp;
<span style="color: #002200;">&#91;</span>popover showRelativeToRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>sender bounds<span style="color: #002200;">&#93;</span> 
ofView<span style="color: #002200;">:</span>sender 
preferredEdge<span style="color: #002200;">:</span>NSMaxYEdge<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The APIs for <strong>NSPopover</strong> are nice and simple, but in true Mac OS fashion the delegate callbacks take the form of <strong>NSNotifications</strong> rather than selectors.</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>popoverDidClose<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>notification
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>notification.object isEqualTo<span style="color: #002200;">:</span>popover<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
   <span style="color: #002200;">&#91;</span>popover release<span style="color: #002200;">&#93;</span>; popover <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>; 
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>If you need to implement something like <strong>NSPopover</strong> in previous versions of Mac OS, then I recommend taking a look at <a href="http://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass">MAAttatchedWindow</a>.</p>
<h2><strong>CoreData</strong></h2>
<p>CoreData has had so many updates in Mac OS Lion it is hard to know where to start. Most of the API changes are to incorporate features that are needed to support Versions and iCloud Syncing. What this means is when you modify files Versions and iCloud only want to know the changes that have been made to a file, so they only need to store or transfer the differences between the original and the new file. This approach not only saves space (only the changes are stored, not another whole file), and it also means the versions of the file can easily be compared and contrasted (I am guessing that Apple uses <a href="http://git-scm.com/">GIT</a> for this). If your application persists any data (and who&#8217;s doesn&#8217;t?), then you must look at CoreData as it gives you so many things for free.</p>
<h4>NSOrderedSet</h4>
<p>An ordered set, isn&#8217;t that just an array? Well in reality it is, it is an array that makes sure that the objects that it holds are unique and therefore not duplicated. That in itself isn&#8217;t that exciting, but what is exciting is that it allows you to have ordered Core Data relationships by simply ticking a box. These ordered relationships do incur a performance penalty over non ordered relationship, so don&#8217;t use them for the sake of it.</p>
<h4>Concurrency</h4>
<p>One of the major issues with CoreData is that <strong>NSManagedObjectContexts</strong> and therefore the <strong>NSManagedObjects</strong> that it contains, are not thread safe. In Lion <strong>NSManagedObjectContext</strong> has the initialiser method <strong>initWithConcurrencyType</strong>, which allows you to tell an <strong>NSManagedObjectContext</strong> to mange all of it&#8217;s interactions using its own private dispatch queue. This means by using <a href="http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html">Grand Central Dispatch</a> you can reduce the complexity of concurrency when using CoreData in Mac OS 10.7.</p>
<h2><strong>Full Screen Windows</strong></h2>
<p>The full screen APIs are incredibly simple to implement (as long as your window resizes to the size of a user&#8217;s screen).</p>
<p>All you need to do is set the window&#8217;s collection behaviour to support full screen:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>window setCollectionBehavior<span style="color: #002200;">:</span>NSWindowCollectionBehaviorFullScreenPrimary<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This one line of code gives you the &#8220;full screen button&#8221; in the top right hand corner of the window.</p>
<p>If you want to make the window go full screen yourself, you just need to call the <strong>toggleFullScreen</strong> method on <strong>NSWindow</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>window toggleFullScreen<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2><strong>View based Table Views</strong></h2>
<p>My favourite new class is without a doubt <strong>NSTableCellView</strong>, as it allows you to use a (subclass of) NSView as a table view cell, rather than an <strong>NSCell</strong> (which is my least favourite class if you are asking). This makes things a lot easier to build custom UIs in a table cell, and also means it can have subviews such as <strong>NSButtons</strong> that can receive mouse events.</p>
<p>The two data source methods you must implement are:</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>NSInteger<span style="color: #002200;">&#41;</span>numberOfRowsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aTableView;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aTableView viewForTableColumn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableColumn</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableColumn row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>row;</pre></div></div>

<p>Although this makes <strong>NSTableView</strong> a lot more like <strong>UITableView</strong> it is important to note that <strong>NSTableView</strong> doesn&#8217;t support sections (as you may have guessed by the <em>numberOfRowsInTableView</em> method not being called <em>numberOfSectionsInTableView</em>). To make things slightly more confusing, <strong>NSTableView</strong> does however support group rows. Group rows float above the non group rows below it, which means they behave like iOS section headers.</p>
<p>You can tell the table view that a cell is a group row by using the delegate 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;">BOOL</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView isGroupRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>row;</pre></div></div>

<p>If you are creating a custom <strong>NSTableCellView</strong> in code, don&#8217;t forget you can override <strong>isFlipped</strong> to flip the coordinate system and make it like the iOS co-ordinate system.</p>
<p>Also don&#8217;t forget that as <strong>NSOutlineView</strong> is a subclass <strong>NSTableView</strong>, so it also supports view based cells.</p>
<h2><strong>In App Purchase</strong></h2>
<p>In App Purchase for iOS has been in the press for all the wrong reasons recently, but it is a great API for Mac OS X developers to have, so they can unlock different features in their applications. The API is very similar to it&#8217;s iOS counterpart besides when it comes to receipt validation. For more information on this I recommend watching the WWDC session 510, which is all about In App Purchase.</p>
<h2><strong>Push Notifications</strong></h2>
<p>Another feature ported across from iOS, but this time with less features. On Mac OS push notifications can only contain a badge value and not an alert and/or sound.</p>
<p>So thats a wrap up of my favourite new APIs in Lion, the standout ones for me being View Based Table Views and <strong>NSPopover</strong>. There have been so many updates I recommend looking through the change list, as you never know what you might find.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/07/20/mac-os-10-7-lion-my-favorite-new-apis/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating singletons using dispatch_once</title>
		<link>http://objcolumnist.com/2011/07/06/creating-singletons-using-dispatch_once/</link>
		<comments>http://objcolumnist.com/2011/07/06/creating-singletons-using-dispatch_once/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 14:49:24 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CocoaTouch]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=447</guid>
		<description><![CDATA[Love them or loathe them, sometimes you need to have a singleton. In fact every iOS and Mac OS application has at least one, UIApplication or NSApplication. So what is a singleton? Wikipedia defines it as: In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, [...]]]></description>
			<content:encoded><![CDATA[<p>Love them or loathe them, sometimes you need to have a singleton. In fact every iOS and Mac OS application has at least one, <strong>UIApplication</strong> or <strong>NSApplication</strong>.</p>
<p>So what is a singleton? <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Wikipedia</a> defines it as:</p>
<blockquote><p>
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object.
</p></blockquote>
<p>Or as I would put it:</p>
<blockquote><p>A singleton is a class, where only one instance of it can instantiated.</p></blockquote>
<p>Although this is the actual definition of a singleton, this isn&#8217;t always the case in the world of Foundation. <strong>NSFileManger</strong> and <strong>NSNotificationCenter</strong> for example, are usually accessed through their class methods <strong>defaultManager</strong> and <strong>defaultCenter</strong> respectively. Although not strictly a singleton, these class methods return a shared instance of that class that developers can then access throughout their code. It is this approach that we will be looking at in this post.</p>
<p>There has always been a debate on the best way to implement the singleton pattern using Objective-C, and developers (including Apple) seem to have been changing their minds every couple of years. When Apple introduced <a href="http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html">Grand Central Dispatch (GCD)</a> (in Mac OS 10.6 and iOS 4.0) they introduced a function that is perfect for implementing the singleton pattern.</p>
<p>This function is <strong>dispatch_once</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">   <span style="color: #a61390;">void</span> dispatch_once<span style="color: #002200;">&#40;</span>
   dispatch_once_t <span style="color: #002200;">*</span>predicate,
   dispatch_block_t block<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>This function takes a predicate (which is a long, that in reality acts as a <strong>BOOL</strong>) that the <strong>dispatch_once</strong> function uses to check if the block has already been dispatched. It also takes the block that you wish to only be dispatched once for the lifetime of the application, for us this is the instantiation of our shared instance.</p>
<p>Not only does <strong>dispatch_once</strong> mean that your code will only ever get run once, it is also thread safe, which means you don&#8217;t have to bother with using anything like <strong>@synchronized</strong> to stop things getting out of sync when using multiple threads and/or queues.</p>
<p>This is verified by Apple&#8217;s <a href="http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html">GCD Documentation</a>:</p>
<blockquote><p>If called simultaneously from multiple threads, this function waits synchronously until the block has completed.</p></blockquote>
<p>So how would you use this in practise?</p>
<p>Well lets say you have a <strong>Account Manager</strong> class, and you want to access a shared instance of this class throughout your application. You can simply implement a class method like the one 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>AccountManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedManager
<span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">static</span> AccountManager <span style="color: #002200;">*</span>sharedAccountManagerInstance <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #a61390;">static</span> dispatch_once_t predicate;
        dispatch_once<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>predicate, <span style="color: #002200;">^</span><span style="color: #002200;">&#123;</span>
                sharedAccountManagerInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>; 
        <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> sharedAccountManagerInstance;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This means whenever you want access this shared instance all you need to do is:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">AccountManager <span style="color: #002200;">*</span>accountManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>AccountManager sharedManager<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And that&#8217;s all there is to it, you now have a shared instance that you can access throughout your application, which will only be created once.</p>
<p>This approach has many advantages:</p>
<ol>
<li>It is thread safe</li>
<li>It will keep the static analyser happy</li>
<li>It is compatible with Automatic Reference Counting (ARC)</li>
<li>It only requires a small amount of code</li>
</ol>
<p>The only disadvantage with this approach is that it will still allow a non shared instance to be created:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">AccountManager <span style="color: #002200;">*</span>accountManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AccountManager alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Sometimes you will actually want this behaviour, but it is something you need to be aware of when you really only want one instance of a class to ever be instantiated.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/07/06/creating-singletons-using-dispatch_once/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WWDC 2011 Keynote Predictions</title>
		<link>http://objcolumnist.com/2011/06/05/wwdc-2011-keynote-predictions/</link>
		<comments>http://objcolumnist.com/2011/06/05/wwdc-2011-keynote-predictions/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 19:49:18 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Keynote]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=428</guid>
		<description><![CDATA[I predict that the keynote will about Mac OS Lion, iOS 5 and iCould &#8230; OK we already know that, so here is what I think that really means. iCloud I think that iCloud will give users the ability to stream any of their iTunes purchased music tracks and videos to an iOS/Mac OS device, [...]]]></description>
			<content:encoded><![CDATA[<p>I predict that the keynote will about Mac OS Lion, iOS 5 and iCould &#8230; OK we already know that, so here is what I think that really means.</p>
<p><strong>iCloud</strong></p>
<p>I think that iCloud will give users the ability to stream any of their iTunes purchased music tracks and videos to an iOS/Mac OS device, for a small subscription fee ($25-$50 per year). I don&#8217;t think they will do what Google and Amazon are currently doing, whereby they allow users to upload their own music regardless of where the files have come from (e.g. burned from a CD), and then stream this to users devices.</p>
<p>I also think that iCloud will incorporate the MobileMe service, and what would make things very interesting is if Apple do something smart with iDisk. If Apple makes iDisk available to every iOS application via a nice and simple API, it would solve two major problems.</p>
<ul>
<li>Getting files on and off of iOS devices</li>
<li>Moving files between applications</li>
</ul>
<p>At the moment a lot of iOS applications use Dropbox for this, but it doesn&#8217;t have to be this way, and Apple will probably want a piece of this pie.</p>
<p><strong>Mac OS Lion</strong></p>
<p>I don&#8217;t think we (developers) will see anything new from Lion in this Keynote (other than anything iCloud related), but they are likely to go over the previously unannounced features such as Version and AirDrop. I think the GM will be given to developers at WWDC, and we will be given a release date &#8230; I think the end of July.</p>
<p><strong>iOS 5</strong><br />
Notifications, Notifications, Notifications. I think Apple have to deal with how applications show notifications. At the moment they force the user to deal with them there and then &#8230; which may have been the simplest option to implement in iOS 1, but every other mobile OS has a better solution in comparison to iOS.</p>
<p>I think in tandem with the new notification system the springboard will get a small update to accommodate them. I&#8217;m not expecting anything major but support for widgets would be nice.</p>
<p>Over the air updates &#8230; I think this may finally happen. For me having to plug my iPhone into my Mac for it to upgrade the OS isn&#8217;t really an issue, it&#8217;s the syncing of apps and music (which I do regularly) that is the major issue. If they allow me to sync over WiFi (or something clever with iCloud) then I will be extremely happy with this as an iOS update.</p>
<p><strong>What there won&#8217;t be</strong></p>
<p>There will be no iPhone 5 (or iPhone 4S or whatever it is called) shown at WWDC, neither will there be any iPad hardware updates. I think the only hardware that we might see unveiled is a Thunderbolt MacBook Air, or a refresh of the Time Capsules to do something clever with iCloud &#8230; it does have a hard drive after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/06/05/wwdc-2011-keynote-predictions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Jump in at the Deep End</title>
		<link>http://objcolumnist.com/2011/06/03/jump-in-at-the-deep-end/</link>
		<comments>http://objcolumnist.com/2011/06/03/jump-in-at-the-deep-end/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 08:18:55 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=412</guid>
		<description><![CDATA[A personal post/announcement that I thought I would share: Today is the last day (for a while at least), that I am a full time employee of someone else&#8217;s company, earning a guaranteed salary every month being an iOS developer. At 5:30pm (maybe a bit earlier &#8230; it is Friday after all) I will become [...]]]></description>
			<content:encoded><![CDATA[<p>A personal post/announcement that I thought I would share:</p>
<p>Today is the last day (for a while at least), that I am a full time employee of someone else&#8217;s company, earning a guaranteed salary every month being an iOS developer. At 5:30pm (maybe a bit earlier &#8230; it is Friday after all) I will become an indie developer, and I will start working full time on a few applications that I have had in mind for quite a while. As much as I tried, I couldn&#8217;t dedicate enough time to these projects while being a full time employee of another company, so I have decided to &#8220;Jump in at the Deep End&#8221; and start working on these projects full time. It&#8217;s a challange and a risk, but one that I am relishing, so hopefully I will make it out the other side in one piece.</p>
<p>If anyone has any advice about running a company (I&#8217;m in the UK), or on being a Indie Software Developer than feel free to share it in the comments section, or @ reply me on Twitter (<a href="http://twitter.com/ObjColumnist">@ObjColumnist</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/06/03/jump-in-at-the-deep-end/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Performing a block of code on a given thread</title>
		<link>http://objcolumnist.com/2011/05/03/performing-a-block-of-code-on-a-given-thread/</link>
		<comments>http://objcolumnist.com/2011/05/03/performing-a-block-of-code-on-a-given-thread/#comments</comments>
		<pubDate>Tue, 03 May 2011 19:29:29 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=373</guid>
		<description><![CDATA[This post has been a long time coming, mainly because I cannot claim that I done any of the work to make it possible, for that I have to thank the Big Nerd Ranch and Landon Fuller. Nevertheless, I have found the following category so useful that I thought I would do a post on [...]]]></description>
			<content:encoded><![CDATA[<p>This post has been a long time coming, mainly because I cannot claim that I done any of the work to make it possible, for that I have to thank the <a href="http://www.informit.com/blogs/blog.aspx?uk=Ask-Big-Nerd-Ranch-Blocks-in-Objective-C">Big Nerd Ranch</a> and <a href="http://landonf.bikemonkey.org/code/iphone/Using_Blocks_1.20090704.html">Landon Fuller</a>. Nevertheless, I have found the following category so useful that I thought I would do a post on it anyway.</p>
<p>When I first started using blocks, I thought <em>&#8220;I have this block of code, can&#8217;t I just run it on a given thread?&#8221;</em>. The answer is (thankfully) yes, but Apple don&#8217;t supply a simple API to do this in Mac OS or iOS. The category below solves this issue:</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;">NSThread</span> <span style="color: #002200;">&#40;</span>MCSMNSThreadCategory<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>MCSM_performBlockOnMainThread<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>block<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> mainThread<span style="color: #002200;">&#93;</span> MCSM_performBlock<span style="color: #002200;">:</span>block<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>MCSM_performBlockInBackground<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>block<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>MCSM_runBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
						      withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>block copy<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>MCSM_runBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>block<span style="color: #002200;">&#123;</span>
	block<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<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>MCSM_performBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>block<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> currentThread<span style="color: #002200;">&#93;</span> isEqual<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	block<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#91;</span>self MCSM_performBlock<span style="color: #002200;">:</span>block waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>MCSM_performBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>block waitUntilDone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>wait<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>MCSM_runBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
					 onThread<span style="color: #002200;">:</span>self
				   withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>block copy<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>
				waitUntilDone<span style="color: #002200;">:</span>wait<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>MCSM_performBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>block afterDelay<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSTimeInterval<span style="color: #002200;">&#41;</span>delay<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>MCSM_performBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
			   withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>block copy<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span> 
			afterDelay<span style="color: #002200;">:</span>delay<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>This category adds a set of (simple) methods to the <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html">NSThread</a> class, that allows you to run a block on any thread that you have a reference to. (You may notice that the Big Nerd Ranch prefix their category methods with BNR and I use MCSM, this is due to Objective-C not supporting namespaces).</p>
<p>For more information on blocks, see <a href="http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html">Apple&#8217;s Blocks Programming Topics</a> documentation.</p>
<p>Alongside blocks Apple introduced <a href="http://developer.apple.com/technologies/mac/snowleopard/gcd.html">Grand Central Dispatch</a> (GCD) in Mac OS 10.6, and I suggest that anyone implementing processor intensive tasks that require code to be executed off of the main thread should try and implement their code using GCD. That being said there are situations (predominantly due to the use of legacy APIs/libraries) whereby the use of a dedicated thread is still required.</p>
<p>Say you have a network thread (<strong>networkThread</strong>), and on this thread is network socket which is maintaining a persistent connection to a server, and a parser for the data that is received on that socket. (There is a common mistake I see, whereby programmers do the network I/O off of the main thread but still parse the data on the main thread, thus locking up the application&#8217;s UI when the data is being parsed).</p>
<p>Normally the data you want to send over the network originates from something that has been triggered from the application&#8217;s UI, and therefore the main thread. You then need to get this data onto the network thread so it can be sent.</p>
<p>Say you wanted to send a person&#8217;s first name (<strong>firstName</strong>), last name (<strong>lastName</strong>) and company name (<strong>companyName</strong>) over the network.</p>
<p>Ideally you would have a method that looked something like:</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>sendFirstName<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>firstName 
lastName<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>lastName 
companyName<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>companyName;</pre></div></div>

<p>This method would convert the data into a format that can be sent over the network (e.g. XML, JSON etc), and queue it on the socket for sending.</p>
<p>The issue is if two threads try to use the same socket at the same time your application will crash, so this method can only ever be called from the <strong>networkThread</strong>. This is also relevant for other non-thread safe APIs.</p>
<p>So for this example we will rename it to:</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>onNetworkThreadSendFirstName<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>firstName 
 lastName<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>lastName 
companyName<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>companyName;</pre></div></div>

<p>and keep the original method for use on the main thread.</p>
<p>So how do you call a method on another thread?<br />
Well you can&#8217;t directly, you have to use:</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>performSelector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector 
 onThread<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSThread</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>thread 
 withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>object 
 waitUntilDone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>wait;</pre></div></div>

<p>The only problem with this is that the selector you call can only take one argument (using the <strong>withObject</strong> parameter), and in this example we want to pass 3.</p>
<p>So what we need to do is put all of the arguments into one object. You could do this using an array (you would need a fixed amount of arguments to do this), a custom object (overkill if it is only going to be used as an argument to a method) or a dictionary which is what I will be using.</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>onNetworkThreadSendArguments<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>arguments;</pre></div></div>

<p>This means that you would end up with an implementation such as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sendFirstName<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>firstName 
 lastName<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>lastName 
 companyName<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>companyName<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>arguments <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
firstName,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>,
lastName,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span>,
companyName,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;companyName&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>self 
performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onNetworkThreadSendArguments<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
onThread<span style="color: #002200;">:</span>networkThread 
withObject<span style="color: #002200;">:</span>arguments 
waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<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>onNetworkThreadSendArguments<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>arguments<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>firstName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arguments objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>lastName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arguments objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>companyName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arguments objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;companyName&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>self 
onNetworkThreadSendFirstName<span style="color: #002200;">:</span>firstName
lastName<span style="color: #002200;">:</span>lastName
companyName<span style="color: #002200;">:</span>companyName<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>onNetworkThreadSendFirstName<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>firstName 
lastName<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>lastName 
companyName<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>companyName<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">//format and send data</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So before 10.6 this technique would be common practise, but it means you have to write extra methods to pack and unpack arguments for cross thread calls.</p>
<p>Thankfully we can get this down to 1 method call using our block category on <strong>NSThread</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sendFirstName<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>firstName 
 lastName<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>lastName 
 companyName<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>companyName<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>networkThread MCSM_performBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">//format and send data</span>
	<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As the block of code is always executed on the network thread, this method can be called from any thread. As you can see blocks can be very useful even when used without Grand Central Dispatch.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/05/03/performing-a-block-of-code-on-a-given-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

