<?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; Objective-C</title>
	<atom:link href="http://objcolumnist.com/category/objective-c/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>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>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>
		<item>
		<title>Mac AppStore Launch</title>
		<link>http://objcolumnist.com/2011/01/04/mac-appstore-launch/</link>
		<comments>http://objcolumnist.com/2011/01/04/mac-appstore-launch/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 13:39:49 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=344</guid>
		<description><![CDATA[The Mac AppStore (MAS) is coming to a Mac near you on the 6th January 2011, but is it going to change the software landscape on the Mac forever ? Number of applications: If there is one thing that we can be sure of, it is that the number of applications available for Mac OS [...]]]></description>
			<content:encoded><![CDATA[<p>The Mac AppStore (MAS) is coming to a Mac near you on the 6th January 2011, but is it going to change the software landscape on the Mac forever ?</p>
<p><strong>Number of applications:</strong><br />
If there is one thing that we can be sure of, it is that the number of applications available for Mac OS will increase due to the introduction of the MAS. There will be iOS developers jumping on board (who are in for a shock when they see NSTableView), and also developers coming &#8220;Back to the Mac&#8221; after spending some time doing iOS development.</p>
<p><strong>Quality of applications:</strong><br />
Quality and Quantity are two very different things. Just because the quantity of applications increases, that doesn’t mean that the number of high quality applications (that we have come to expect on the Mac) will increase accordingly. I (unfortunately) think that there will be a lot of substandard applications released for the Mac, simply because of the MAS making distribution easier. I do however think that there will be a few gems uncovered because of it.</p>
<p><strong>Price of applications:</strong><br />
When the Mac AppStore was first announced, developers contemplated raising their prices to compensate for Apple taking a 30% cut of the revenue (compared to &lt;10% for most other providers), now developers are thinking the opposite &#8230; lets lower prices. I don&#8217;t think that this will be as extreme as the &#8220;race to the bottom&#8221; on the iOS AppStore, as Mac applications usually take longer to build than there iOS equivalents. This is mainly because iOS has a more modern set of APIs, and on the Mac you have to support extras like Drag and Drop, Keyboard Shortcuts etc.</p>
<p>I think that Mac AppStore application pricing will fall into 4 main categories:</p>
<ul>
<li>&lt; $5 &#8211; Simple single purpose applications</li>
<li>$10 &#8211; Simple applications with an iOS sized feature set</li>
<li>$20 &#8211; $40 Fully featured applications</li>
<li>$40+ Professional applications like Microsoft Office, Adobe creative suite etc</li>
</ul>
<p><strong>Application Sales:</strong><br />
The simple fact is that people are more willing to give their payment details to Apple, rather than entering them on an indie developer&#8217;s website. If, and it is a big if, you get a prime place on the Mac AppStore (featured,top 25s etc) your sales are likely to be huge. For all the other applications on AppStore, time will only tell if it dramatically increases sales beyond the number of sales gained from the security of purchasing through Apple. I do believe however that non MAS sales will suffer a lot. The people that currently purchase applications online are likely to be &#8220;tech savy&#8221;, and therefore they will know about the MAS. Why would these users not switch to purchasing all of their applications through Apple ? the fact is they will.</p>
<p><strong>Hello AppStore bye bye Serial codes:</strong><br />
There are a few benefits of distributing applications through the AppStore rather than handling distribution yourself. One of the major pains is payments and serials, which can take up a considerable amount of an application&#8217;s development time. This along with handling updates, is now removed from a developers workload with the introduction of the MAS, saving developers time and a lot of headaches.</p>
<p><strong>Conclusion:</strong><br />
On the whole I can only see the Mac AppStore being a positive thing for Mac users and developers alike. Developers have a central place to sell their products from, and they don&#8217;t have to worry about getting it listed on numerous application sites. Users will have a an application (pre installed on their Mac !!!) which will allow them to search, view and securely purchase thousands of applications using just their Apple ID. </p>
<p>This could be a very big year for the Mac&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2011/01/04/mac-appstore-launch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Detecting Multitouch Gestures in iOS</title>
		<link>http://objcolumnist.com/2010/11/08/detecting-multitouch-gestures-in-ios/</link>
		<comments>http://objcolumnist.com/2010/11/08/detecting-multitouch-gestures-in-ios/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 19:33:17 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[CocoaTouch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=284</guid>
		<description><![CDATA[Before iOS 3.2 was introduced on the iPad (and latter with iOS 4.0 on the iPhone and iPod Touch), interpreting touch events as different gestures was an awkward task, and relied upon subclassing UIView, and then implementing the following methods. - &#40;void&#41;touchesBegan:&#40;NSSet *&#41;touches withEvent:&#40;UIEvent *&#41;event; - &#40;void&#41;touchesMoved:&#40;NSSet *&#41;touches withEvent:&#40;UIEvent *&#41;event; - &#40;void&#41;touchesEnded:&#40;NSSet *&#41;touches withEvent:&#40;UIEvent *&#41;event; [...]]]></description>
			<content:encoded><![CDATA[<p>Before iOS 3.2 was introduced on the iPad (and latter with iOS 4.0 on the iPhone and iPod Touch), interpreting touch events as different gestures was an awkward task, and relied upon subclassing UIView, and then implementing the following methods.</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>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesMoved<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesCancelled<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;</pre></div></div>

<p>If you were trying to detect if a user was dragging a view (or panning as Apple would say), you would usually implement the <strong>touchesBegan:withEvent:</strong> to start dragging the piece, <strong>touchesMoved:withEvent:</strong> to adjust the view&#8217;s frame, and then <strong>touchesEnded:withEvent:</strong> and <strong>touchesCancelled:withEvent:</strong> to stop detecting the drag. While implementing dragging was not too complex, trying to detect a more advance gesture such as a swipe or pinch was a lot more complex &#8230; and a lot of hard work to make it replicates Apple&#8217;s version of the gestures accurately. </p>
<p>Thankfully Apple created <strong>UIGestureRecognizer</strong>:</p>
<blockquote><p>
UIGestureRecognizer is an abstract base class for concrete gesture-recognizer classes. A gesture-recognizer object (or, simply, a gesture recognizer) decouples the logic for recognizing a gesture and acting on that recognition. When one of these objects recognizes a common gesture or, in some cases, a change in the gesture, it sends an action message to each designated target object.</p>
<p>Taken from <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html<br />
">Apple</a>
</p></blockquote>
<p>As <strong>UIGestureRecognizer</strong> is an abstract class, you will never use it directly. You will however use one of its subclasses, that have been designed specifically to capture a specific gesture.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer</pre></div></div>

<p>So lets take <strong>UIPanGestureRecognizer</strong> for an example, and we will use it to move a view around.</p>
<p>Our example view controller has a view (obviously), and this has a subview called <strong>panView</strong>, which is the view we will move around.</p>
<p>First thing that you will need to do, is create a pan gesture recognizer (<strong>UIPanGestureRecognizer</strong>) and add it to the <strong>panView</strong>. This will allow you to detect the gesture. To get informed about the gesture getting detected, you have to set the view controller as the delegate, and then have it call the the<strong> panViewWithGestureRecognizer:</strong> selector. (Note: You can set any object as the delegate, as long as it implements <strong>UIGestureRecognizerDelegate</strong> protocol. For this example the view controller makes sence.)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIPanGestureRecognizer <span style="color: #002200;">*</span>panGesture <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
panGesture <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIPanGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>panViewWithGestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>panGesture setMaximumNumberOfTouches<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>panGesture setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>panView addGestureRecognizer<span style="color: #002200;">:</span>panGesture<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>panGesture release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><strong>panViewWithGestureRecognizer:</strong> will get called when the gesture changes state, the possible states are:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
UIGestureRecognizerStatePossible  
UIGestureRecognizerStateBegan    
UIGestureRecognizerStateChanged   
UIGestureRecognizerStateEnded    
UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled   
UIGestureRecognizerStateFailed
UIGestureRecognizerStateRecognized
UIGestureRecognizerStateEnded</pre></div></div>

<p>For this example we only care about if gesture began (<strong>UIGestureRecognizerStateBegan</strong>) or has changed (<strong>UIGestureRecognizerStateChanged</strong>). When a pan gesture happens (begins or changes) we want to move the center of <strong>panView</strong> to be underneath the users finger, thus dragging the piece. </p>
<p>To do this we implement<strong panViewWithGestureRecognizer:</strong> in the following way.</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>panViewWithGestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIPanGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gestureRecognizer
<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>gestureRecognizer state<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIGestureRecognizerStateBegan 
         || <span style="color: #002200;">&#91;</span>gestureRecognizer state<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIGestureRecognizerStateChanged<span style="color: #002200;">&#41;</span> 
    <span style="color: #002200;">&#123;</span>
&nbsp;
        CGPoint translation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>gestureRecognizer translationInView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>panView superview<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span>piece setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>panView center<span style="color: #002200;">&#93;</span>.x <span style="color: #002200;">+</span> translation.x, <span style="color: #002200;">&#91;</span>panView center<span style="color: #002200;">&#93;</span>.y <span style="color: #002200;">+</span> translation.y<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>gestureRecognizer setTranslation<span style="color: #002200;">:</span>CGPointZero inView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>panView superview<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>What makes this super easy is the convenience method<strong>translationInView:</strong>, which is found on <strong>UIPanGestureRecognizer</strong>. This gives you the value of where the center of the <strong>panView</strong> should be, without having to calculate it yourself. We then need to set the translation back to zero using <strong>setTranslation:</strong>, so when we calculate the next gesture, it will be from the new position of the <strong>panView</strong>. And thats all there is to it.</p>
<p>As mentioned before there are a few gesture recogniser that have already been prebuilt for you to use, and you can even add multiple gesture recogniser to the same view. If you do this, you will want to implement:</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>gestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>otherGestureRecognizer;</pre></div></div>

<p>This simply allows you to specify if a gesture should be detected alongside another gesture e.g. a pinch and a rotation gesture. If Apple&#8217;s pre-built gesture recognizers are not enough for you, you can always create your own subclass of <strong>UIGestureRecognizer</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2010/11/08/detecting-multitouch-gestures-in-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Core Animation using blocks</title>
		<link>http://objcolumnist.com/2010/09/19/core-animation-using-blocks/</link>
		<comments>http://objcolumnist.com/2010/09/19/core-animation-using-blocks/#comments</comments>
		<pubDate>Sun, 19 Sep 2010 11:27:46 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[CocoaTouch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=244</guid>
		<description><![CDATA[Previously I have written a blog post about doing simple animations with UIViews, which you can find here. Starting in iOS 4.0 you can now do this with blocks. Blocks have two major benefits (although there are more): They can be used in conjunction with Grand Central Dispatch (GCD), as an alternative to threading. They [...]]]></description>
			<content:encoded><![CDATA[<p>Previously I have written a blog post about doing simple animations with <strong>UIView</strong>s, which you can find <a href="http://objcolumnist.com/2009/07/18/simple-uiview-based-animations-on-the-iphone/">here</a>. Starting in <strong>iOS 4.0</strong> you can now do this with blocks. </p>
<p>Blocks have two major benefits (although there are more):</p>
<ul>
<li>
They can be used in conjunction with<strong> Grand Central Dispatch (GCD)</strong>, as an alternative to threading.
</li>
<li>
They can be used for callbacks, instead of <strong>NSNotification</strong>s, callback selectors, function pointers etc. This is usually done by providing a callback/completion block, and this is what we will be looking at here.
</li>
</ul>
<p>The most common method that you will use to animate views using blocks, is the following class method on <strong>UIView</strong>:</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>animateWithDuration<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSTimeInterval<span style="color: #002200;">&#41;</span>duration animations<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: #a61390;">void</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>animations completion<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: #a61390;">BOOL</span> finished<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>completion;</pre></div></div>

<p>This method allows you to set a time interval, along with an animations block and a completion block.</p>
<p>Lets say we have a <strong>UIView</strong> variable called <em>redView</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">redView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">320</span>,<span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
redView.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor redColor<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>We will then add <em>redView</em> as a subview of the view controller&#8217;s view</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>redView<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>A common situation you may find your self in, is when you finish animating a view (e.g. changing its alpha), you would then want to remove it from it&#8217;s superview, and then release it from memory.</p>
<p>So if you had (for example) a method called <em>animate</em> to trigger off the animation, you would need to add an &#8220;animation did stop selector&#8221; to the animation. In the following example I have called it <em>cleanUp</em>.</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>animate<span style="color: #002200;">&#123;</span>
&nbsp;
   <span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">5.0</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>UIView setAnimationDidStopSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>cleanUp<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>redView setAlpha<span style="color: #002200;">:</span><span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>UIView commitAnimations<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>cleanUp<span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>redView removeFromSuperview<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">//I do release and = nil on the same line as a coding convention</span>
    <span style="color: #11740a; font-style: italic;">//so I don't forget to &quot;nil&quot; the variable</span>
    <span style="color: #002200;">&#91;</span>redView release<span style="color: #002200;">&#93;</span>, redView <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;	
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Although this works well enough, it does mean that your code is split up into the animation code and the (completion) clean up code. If you end up doing this a lot, your code can end up becoming very fragmented and hard to follow.</p>
<p>Using blocks we can do the following:</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>animate<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>UIView animateWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">5.0</span>
&nbsp;
                            animations<span style="color: #002200;">:^</span><span style="color: #002200;">&#123;</span> 
	                          redView.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>; 
	                     <span style="color: #002200;">&#125;</span>
&nbsp;
                           completion<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span>  completed<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	                          <span style="color: #002200;">&#91;</span>redView removeFromSuperview<span style="color: #002200;">&#93;</span>;
                                  <span style="color: #002200;">&#91;</span>redView release<span style="color: #002200;">&#93;</span>,redView <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	                    <span style="color: #002200;">&#125;</span>
 <span style="color: #002200;">&#93;</span>;
&nbsp;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This keeps things nice and simple, and it means that you can easily see what code will be executed when the animation completes. </p>
<p>All of the animations you wish to do are passed in using a block:</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;">&#123;</span> 
redView.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>; 
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This block takes no arguments, and it also does not have a return type (so it defaults to void).</p>
<p>Instead of calling the <em>cleanUp</em> selector, we can simply pass in a completion block:</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;">BOOL</span>  completed<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#91;</span>redView removeFromSuperview<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>redView release<span style="color: #002200;">&#93;</span>,redView <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This block takes one parameter, which is a bool signifying if the animation has completed when the block is executed. In this example we ignore the completed variable, and we assume that the animation has completed for simplicity reasons. In the body of the block we do same as we did in the <em>cleanUp</em> selector, we remove <em>redView</em> from its super view and then release it from memory.</p>
<p>In some situations you won&#8217;t need to use blocks for Core Animation, but when you do, you will find it cleans up your code no end.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2010/09/19/core-animation-using-blocks/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Retrieving the currently being played music track</title>
		<link>http://objcolumnist.com/2010/07/11/retrieving-the-currently-being-played-music-track/</link>
		<comments>http://objcolumnist.com/2010/07/11/retrieving-the-currently-being-played-music-track/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 11:48:18 +0000</pubDate>
		<dc:creator>Spencer MacDonald</dc:creator>
				<category><![CDATA[CocoaTouch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://objcolumnist.com/?p=213</guid>
		<description><![CDATA[One question I keep hearing is &#8220;Can you find out what music track a user is listening to? as I want to use it for &#8230;&#8221;, where the reason usually revolves around posting it to a social network network site, or using it as IM status. Thankfully retrieving the currently being played music track is [...]]]></description>
			<content:encoded><![CDATA[<p>One question I keep hearing is &#8220;Can you find out what music track a user is listening to? as I want to use it for &#8230;&#8221;, where the reason usually revolves around posting it to a social network network site, or using it as IM status. Thankfully retrieving the currently being played music track is very easy thanks to the <strong>MediaPlayer Framework</strong>.</p>
<p>Each application has its own <strong>MPMusicPlayerController</strong>, but it also has access to the iPod&#8217;s <strong>MPMusicPlayerController</strong>, using the class method <strong>iPodMusicPlayer</strong>.</p>

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

<p>After you have got the iPod music player, you can then get the <strong>now playing item</strong></p>

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

<p>If the now playing item is <strong>nil</strong>, you know that the user is not playing a music track on their iPod.</p>
<p>Unlike many of the other APIs in iOS, you can&#8217;t access information such as the track name, via a simple string property. You have to use one of the following keys:</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> MPMediaItemPropertyPersistentID;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyMediaType; 
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyTitle;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyAlbumTitle; 
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyArtist; 
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyAlbumArtist; 
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyGenre;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyComposer; 
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyPlaybackDuration;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyAlbumTrackNumber;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyAlbumTrackCount;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyDiscNumber;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyDiscCount;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyArtwork;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyLyrics;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyIsCompilation;  
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyReleaseDate;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyBeatsPerMinute;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyComments;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> MPMediaItemPropertyAssetURL;</pre></div></div>

<p>And then query the Media Player Item, using the instance method <strong>valueForProperty:</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>itemTitle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nowPlayingItem valueForProperty<span style="color: #002200;">:</span>MPMediaItemPropertyTitle<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And thus you will end up with a code snippet like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">MPMusicPlayerController <span style="color: #002200;">*</span>iPodMusicPlayerController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>MPMusicPlayerController iPodMusicPlayer<span style="color: #002200;">&#93;</span>;
&nbsp;
MPMediaItem <span style="color: #002200;">*</span>nowPlayingItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>iPodMusicPlayerController nowPlayingItem<span style="color: #002200;">&#93;</span>;	
&nbsp;
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>nowPlayingItem<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>itemTitle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nowPlayingItem valueForProperty<span style="color: #002200;">:</span>MPMediaItemPropertyTitle<span style="color: #002200;">&#93;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User is playing the following song: %@&quot;</span>,itemTitle<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span><span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User is not playing a song&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As always this is just a small code snippet to get you started. There are situations for instance, where the user can have a <strong>now playing item</strong> that has no title (strange I know). So as always you will have to handle these edge cases appropriately.</p>
]]></content:encoded>
			<wfw:commentRss>http://objcolumnist.com/2010/07/11/retrieving-the-currently-being-played-music-track/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

