<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:rawvoice="http://www.rawvoice.com/rawvoiceRssModule/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The WP Guru » iOS Development</title>
	
	<link>http://wpguru.co.uk</link>
	<description>Complicated Stuff. Made Simple.</description>
	<lastBuildDate>Fri, 17 May 2013 02:56:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
<!-- podcast_generator="Blubrry PowerPress/4.0.7" -->
	<itunes:summary>Complicated Stuff. Made Simple.</itunes:summary>
	<itunes:author>Jay Versluis</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://wpguru.co.uk/wp-content/uploads/2013/02/WP-Guru-Logo-iTunes-Logo.jpg" />
	<itunes:owner>
		<itunes:name>Jay Versluis</itunes:name>
		<itunes:email>jay@versluis.com</itunes:email>
	</itunes:owner>
	<managingEditor>jay@versluis.com (Jay Versluis)</managingEditor>
	<copyright>2013 by Jay Versluis</copyright>
	<itunes:subtitle>Complicated Stuff. Made Simple.</itunes:subtitle>
	<itunes:keywords>WordPress, Dreamweaver, iOS Development, php, mySQL, JavaScript, How-To, how to</itunes:keywords>
	<image>
		<title>The WP Guru » iOS Development</title>
		<url>http://wpguru.co.uk/wp-content/uploads/2013/02/WP-Guru-Logo-iTunes-Logo.jpg</url>
		<link>http://wpguru.co.uk/category/ios/</link>
	</image>
	<itunes:category text="Technology">
		<itunes:category text="Software How-To" />
	</itunes:category>
		<rawvoice:location>Miami Beach, FL</rawvoice:location>
		<rawvoice:frequency>weekly(ish)</rawvoice:frequency>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/IosDevDiary" /><feedburner:info uri="iosdevdiary" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>How to create an NSDate object</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/GXQFK6tzVtE/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-create-an-nsdate-object/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 15:34:41 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateFormatter]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-an-nsdate-object/</guid>
		<description><![CDATA[The easiest way to create an NSDate object is to create &#8220;right now&#8221; with our convenience method date: NSDate *myDate = [NSDate date]; But if you want to create a date object with a date such as your birthday it gets a little bit trickier, and &#8211; more importantly &#8211; much less obvious. To do [...]]]></description>
				<content:encoded><![CDATA[<p>The easiest way to create an NSDate object is to create &#8220;right now&#8221; with our convenience method date:</p>
<pre class="wp-code-highlight prettyprint">
NSDate *myDate = [NSDate date];
</pre>
<p>But if you want to create a date object with a date such as your birthday it gets a little bit trickier, and &#8211; more importantly &#8211; much less obvious. </p>
<p>To do this, we need to create an NSDateFormatter, tell the formatter how to expect the date, and then use its convenience method dateFromString to create the date:</p>
<pre class="wp-code-highlight prettyprint">
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @&quot;YYYY-MM-DD&quot;;
NSDate *myDate = [formatter dateFromString:@&quot;2012-12-12&quot;];
</pre>
<p>Notice that I tell the date formatter to expect the date as YYYY-MM-DD. I could also have told it to expect it as YYMMDD and then pass @&#8221;12-12-12&#8243; in the dateFromString method. </p>
<p>Whichever way you do it, the date format must match your string, otherwise the method returns null.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/GXQFK6tzVtE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-create-an-nsdate-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-create-an-nsdate-object/</feedburner:origLink></item>
		<item>
		<title>How to display the full month from an NSDate (such as “February”)</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/3xS6Dg4UA-U/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-display-the-full-month-from-an-nsdate-such-as-february/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 15:15:14 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateFormatter]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-display-the-full-month-from-an-nsdate-like-february/</guid>
		<description><![CDATA[There was me thinking I&#8217;d have to whip out NSDateComponents and NSCalendar &#8211; but my mind works too complicated at times. All we need to do this is our good friend the NSDateFormatter and the Unicode Date Format MMMM &#8211; let&#8217;s check it out: // create today's date NSDate *myDate = [NSDate date]; // create [...]]]></description>
				<content:encoded><![CDATA[<p>There was me thinking I&#8217;d have to whip out NSDateComponents and NSCalendar &#8211; but my mind works too complicated at times. </p>
<p>All we need to do this is our good friend the NSDateFormatter and the Unicode Date Format MMMM &#8211; let&#8217;s check it out:</p>
<pre class="wp-code-highlight prettyprint">
// create today&#039;s date
NSDate *myDate = [NSDate date];
// create a date formatter and set its format
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @&quot;MMMM&quot;;
        
NSLog(@&quot;The current month is %@&quot;, [formatter stringFromDate:myDate]);
</pre>
<p>The MMMM tells the date formatter to display the full month, no matter how many letters it has. You can also use:</p>
<ul>
<li>M for the month number (1 for January or 12 for December)</li>
<li>MM for the two-digit month number (01 for January)</li>
<li>MMM for the abbreviated month number (Jan for January)</li>
<li>MMMM for the full month number (such as January)</li>
<li>MMMMM for just the first letter (J for January&#8230; not sure who would need this though)</li>
</ul>
<p>The Unicode Date Format can do amazing things with all aspects of times and dates &#8211; check out the complete compendium here:</p>
<p><a href="http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns" rel="nofollow">http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns</a></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/3xS6Dg4UA-U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-display-the-full-month-from-an-nsdate-such-as-february/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-display-the-full-month-from-an-nsdate-such-as-february/</feedburner:origLink></item>
		<item>
		<title>How to create Linkshare Affiliate Links to iTunes Products</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/JT779s_ZZr4/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-create-linkshare-affiliate-links-to-itunes-products/#comments</comments>
		<pubDate>Sat, 27 Apr 2013 18:16:30 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-linkshare-affiliate-links-to-itunes-products/</guid>
		<description><![CDATA[One of the worst ever interfaces i have ever come across is that of Rakuten LinkShare. Even before they&#8217;ve been bought by Japanese giant Rakuten it was bad, and it hasn&#8217;t been improved since I&#8217;ve signed up with them in 2008. Lucky for us we don&#8217;t have to use their interface to get links to [...]]]></description>
				<content:encoded><![CDATA[<p>One of the worst ever interfaces i have ever come across is that of Rakuten LinkShare. Even before they&#8217;ve been bought by Japanese giant Rakuten it was bad, and it hasn&#8217;t been improved since I&#8217;ve signed up with them in 2008.</p>
<p>Lucky for us we don&#8217;t have to use their interface to get links to iTunes Products &#8211; we can use the standard good looking and well working iTunes Link Maker. All we need is go there via LinkShare, which will embed our affiliate code.</p>
<p>Here&#8217;s how to do this step by step:</p>
<p>1.) Login to LinksShare and see the awful Home Screen<br />
2.) Head over to Links &#8211; Get Links<br />
3.) Click on &#8220;US iTunes, App Store, iBookstore, and Mac App Store&#8221; (you need to be subscribed and approved for this option to become available).</p>
<p>This should get you to a screen similar to this:</p>
<p>&nbsp;</p>
<p><a href="http://pinkstone.co.uk/wp-content/uploads/2013/03/Screen-Shot-2013-04-27-at-14.09.41.png"><img class="aligncenter size-large wp-image-752" alt="Screen Shot 2013-04-27 at 14.09.41" src="http://pinkstone.co.uk/wp-content/uploads/2013/03/Screen-Shot-2013-04-27-at-14.09.41-600x367.png" width="600" height="367" /></a></p>
<p>&nbsp;</p>
<p>4.) Notice the grey button that says &#8220;Link Maker Tool&#8221;. Click that and you get to a new window and is the familiar iTunes Link Builder, as provided by Apple:</p>
<p>&nbsp;</p>
<p><a href="http://pinkstone.co.uk/wp-content/uploads/2013/04/Screen-Shot-2013-04-27-at-14.12.01.png"><img class="aligncenter size-large wp-image-755" alt="Screen Shot 2013-04-27 at 14.12.01" src="http://pinkstone.co.uk/wp-content/uploads/2013/04/Screen-Shot-2013-04-27-at-14.12.01-600x342.png" width="600" height="342" /></a></p>
<p>&nbsp;</p>
<p>Note the message that reads: &#8220;Your affiliate Network is&#8230;&#8221; &#8211; this is the confirmation that you&#8217;re building links as an affiliate. If that message isn&#8217;t there, your links are not tracked. Instead if LinkShare, this can be a different partner (TradeDoubler is another one for example).</p>
<p>Now you can enter a product and grab the link, which will already contain your affiliate ID.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/JT779s_ZZr4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-create-linkshare-affiliate-links-to-itunes-products/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-create-linkshare-affiliate-links-to-itunes-products/</feedburner:origLink></item>
		<item>
		<title>How to create a UIBarButtonItem in code and make it call a method</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/muD_Kq93tFo/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-create-a-uibarbuttonitem-in-code-and-make-it-call-a-method/#comments</comments>
		<pubDate>Sat, 27 Apr 2013 17:37:06 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[UIBarButton]]></category>
		<category><![CDATA[UINavigationBar]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-a-uibarbuttonitem-in-code-and-make-it-call-a-method/</guid>
		<description><![CDATA[Some things are really easy to do via a Storyboard &#8211; but when you want to create the same thing in code I frequently forget how to do it. Turns out it&#8217;s equally simple: this example assumes you have a View Controller which is embedded in a Navigation Controller (so it already has a UINavigationBar [...]]]></description>
				<content:encoded><![CDATA[<p>Some things are really easy to do via a Storyboard &#8211; but when you want to create the same thing in code I frequently forget how to do it. </p>
<p>Turns out it&#8217;s equally simple: this example assumes you have a View Controller which is embedded in a Navigation Controller (so it already has a UINavigationBar at the top). Here&#8217;s how you add a button to it, set the title and target, and tell it what to do when it&#8217;s pressed:</p>
<pre class="wp-code-highlight prettyprint">
- (void) viewDidLoad
{
    [super viewDidLoad];
	
    // first we create a button and set it&#039;s properties
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc]init];
    myButton.action = @selector(doTheThing);
    myButton.title = @&quot;Hello&quot;;
    myButton.target = self;
    
    // then we add the button to the navigation bar
    self.navigationItem.rightBarButtonItem = myButton;
    
    
}


// method called via selector
- (void) doTheThing {
    
    NSLog(@&quot;Doing the thing&quot;);
    
}
</pre>
<p>You can set the button on the left hand site by using self.navigationItem.leftBarButtonItem</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/muD_Kq93tFo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-create-a-uibarbuttonitem-in-code-and-make-it-call-a-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-create-a-uibarbuttonitem-in-code-and-make-it-call-a-method/</feedburner:origLink></item>
		<item>
		<title>How to remove a Core Data Model Version</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/InwpSOQsLKE/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-remove-a-core-data-model-version/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 20:57:41 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-remove-a-core-data-model-version/</guid>
		<description><![CDATA[Core Data can handle several versions of your Model. It&#8217;s easy to add a version (via Editor &#8211; Add Model Version) and set it active, but it&#8217;s not so easy to remove a version you no longer need. Thankfully there is a way to delete version files which goes a long way to declutter your [...]]]></description>
				<content:encoded><![CDATA[<p>Core Data can handle several versions of your Model. It&#8217;s easy to add a version (via Editor &#8211; Add Model Version) and set it active, but it&#8217;s not so easy to remove a version you no longer need.</p>
<p>Thankfully there is a way to delete version files which goes a long way to declutter your brain. The secret lies in the fact that the .xcdatamodeld file is actually a Package and can contain more than one file. It&#8217;s like the .app extension which isn&#8217;t just one file. I never knew this!</p>
<p>To explore, select your versioned .xcdatamodeld file, right-click it and select &#8220;Show in Finder&#8221;. Once in there right-click it again and select &#8220;Show Package Contents&#8221;. Surprise &#8211; here are all your Model Versions. Before you go on a mad deleting spree, head back to Xcode and do the following:</p>
<ol>
<li>activate a Model Version that you want to keep (by going to the top level .xcdatamodeld entry, then select it under &#8220;Versioned Core Data Model&#8221;)</li>
<li>next remove the entire file from your project (just the reference&#8230; do not move to trash)</li>
<li>head back to the Finder and delete the files inside the Package you no longer want</li>
<li>go back to Xcode and add the .xcdatamodeld file again</li>
</ol>
<p>Voila &#8211; now all your unnecessary Model Versions are gone. Perhaps in a future version of Xcode there will be an easier way to do this, but as of Xcode 4.6.2 (April 2013) there is not.</p>
<h3>Further Reading</h3>
<p>Thanks to David Avendasora for this wonderfully simple explanation on Stack Overflow: <a href="http://stackoverflow.com/questions/7708392/how-to-delete-an-old-unused-data-model-version-in-xcode-4" rel="nofollow">http://stackoverflow.com/questions/7708392/how-to-delete-an-old-unused-data-model-version-in-xcode-4</a></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/InwpSOQsLKE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-remove-a-core-data-model-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-remove-a-core-data-model-version/</feedburner:origLink></item>
		<item>
		<title>How to create a View Controller defined in you your Storyboard programmatically</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/mwxeuuLs7BE/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-create-a-view-controller-defined-in-you-your-storyboard-programmatically/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 13:20:39 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-a-view-controller-defined-in-you-your-storyboard-programmatically/</guid>
		<description><![CDATA[Your View Controllers are created by the Storyboard automatically depending their defined relationships in Interface Builder. Sometimes however we need to create and transition to View Controllers we&#8217;ve defined in code. For example, if you want to transition to a view as part of displaying a search result. We can do this by creating a [...]]]></description>
				<content:encoded><![CDATA[<p>Your View Controllers are created by the Storyboard automatically depending their defined relationships in Interface Builder. </p>
<p>Sometimes however we need to create and transition to View Controllers we&#8217;ve defined in code. For example, if you want to transition to a view as part of displaying a search result. </p>
<p>We can do this by creating a new UIStoryboard object and then asking it to create a View Controller defined in it. For this to work you need to give your View Controller a unique identifier using the Identity Inspector (under Identity, set a Storyboard ID).</p>
<p>This example assumes we have a Storyboard file called MainStoryboard.storyboard in which there&#8217;s a View Controller called DetailView. Here&#8217;s how we create it and push it onto a stack of Navigation Controllers:</p>
<pre class="wp-code-highlight prettyprint">
// create the Storyboard object
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@&quot;MainStoryboard&quot; bundle:nil];
DetailViewController *detailView = [storyboard instantiateViewControllerWithIdentifier:@&quot;DetailView&quot;];

// set properties you&#039;d like the detailView to have (optional)
detailView.someProperty = @&quot;This is great&quot;;

// push the new detailView onto the stack
[self.navigationController pushViewController:detailView animated:YES];
</pre>
<p>Note that you define the Storyboard without its extension. If the bundle parameter is nil (as it is here) then the compiler assumes your main bundle.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/mwxeuuLs7BE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-create-a-view-controller-defined-in-you-your-storyboard-programmatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-create-a-view-controller-defined-in-you-your-storyboard-programmatically/</feedburner:origLink></item>
		<item>
		<title>How to add a Search Display Controller to a UITableView (in code)</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/QbOIVzTbQww/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-add-a-search-display-controller-to-a-uitableview-in-code/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 03:39:32 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[UISearchBar]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-add-a-search-display-controller-to-a-uitableview-in-code/</guid>
		<description><![CDATA[We&#8217;ve recently discussed how to deal with a Search Bar and Search Display Controller using Interface Builder. You can however do this in code too. This can be useful if you don&#8217;t want to make all the relevant connections in every Storyboard. This example assumes you have a Table View (self.tableView) to which you&#8217;d like [...]]]></description>
				<content:encoded><![CDATA[<p>We&#8217;ve recently discussed how to deal with a Search Bar and Search Display Controller using Interface Builder. You can however do this in code too. This can be useful if you don&#8217;t want to make all the relevant connections in every Storyboard.</p>
<p>This example assumes you have a Table View (self.tableView) to which you&#8217;d like to add a Search Bar and Search Display Controller at the top. We do this by utilising the Table View&#8217;s tableHeaderView property:</p>
<pre class="wp-code-highlight prettyprint">
// create a new Search Bar and add it to the table view
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
self.tableView.tableHeaderView = searchBar;
        
// we need to be the delegate so the cancel button works
searchBar.delegate = self;
        
// create the Search Display Controller with the above Search Bar
self.controller = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.controller.searchResultsDataSource = self;
self.controller.searchResultsDelegate = self;
</pre>
<p>Your View Controller also needs to conform to the UISearchBarDelegate Protocol for this to work properly.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/QbOIVzTbQww" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-add-a-search-display-controller-to-a-uitableview-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-add-a-search-display-controller-to-a-uitableview-in-code/</feedburner:origLink></item>
		<item>
		<title>How to parse a JSON URL in iOS and Cocoa</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/hPU8JtvAO04/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-parse-a-json-url-in-ios-and-cocoa/#comments</comments>
		<pubDate>Sun, 14 Apr 2013 13:03:28 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSURL]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-parse-a-json-url-in-ios-and-cocoa/</guid>
		<description><![CDATA[Both Cocoa and iOS have built-in methods to parse (and generate) JSON data using NSJSONSerialization. Most commonly you&#8217;ll want to call a URL with parameters which will return some data. You take this output and tell Objective-C that it&#8217;s JSON, then typecast the return to an array or a dictionary which in turn lets you [...]]]></description>
				<content:encoded><![CDATA[<p>Both Cocoa and iOS have built-in methods to parse (and generate) JSON data using NSJSONSerialization. </p>
<p>Most commonly you&#8217;ll want to call a URL with parameters which will return some data. You take this output and tell Objective-C that it&#8217;s JSON, then typecast the return to an array or a dictionary which in turn lets you access the data. It&#8217;s genius!</p>
<p>Here&#8217;s an example:</p>
<pre class="wp-code-highlight prettyprint">
// create the URL we&#039;d like to query
NSURL *myURL = [[NSURL alloc]initWithString:@&quot;<a href="http://domain.com/jsonquery&quot;" rel="nofollow">http://domain.com/jsonquery&quot;</a>;
        
// we&#039;ll receive raw data so we&#039;ll create an NSData Object with it 
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
        
// now we&#039;ll parse our data using NSJSONSerialization
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
        
// typecast an array and list its contents
NSArray *jsonArray = (NSArray *)myJSON;
        
// take a look at all elements in the array
for (id element in jsonArray) {
    NSLog(@&quot;Element: %@&quot;, [element description]);            
}
</pre>
<p>What you&#8217;ll get back depends on your individual JSON structure of course. This can be quite complex, for example you could receive an array full of dictionaries, each of which could be full of dictionaries. JSON data can be mapped to an NSArray, NSDictionary, NSNumberand NSNull.</p>
<p>Once you have access to the top level elements you can then loop through each one, examining what&#8217;s in it and access what you need.</p>
<h3>Further Reading</h3>
<ul>
<li>More details about the JSON Format: <a href="http://www.json.org/" rel="nofollow">http://www.json.org/</a></li>
<li>NSJSONSerialization Class Reference: <a href="http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html" rel="nofollow">http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html</a></li>
<li>JSONKit Framework: <a href="https://github.com/johnezang/JSONKit" rel="nofollow">https://github.com/johnezang/JSONKit</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/hPU8JtvAO04" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-parse-a-json-url-in-ios-and-cocoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-parse-a-json-url-in-ios-and-cocoa/</feedburner:origLink></item>
		<item>
		<title>How to use a Selector</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/ClL_DCJ84zw/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-use-a-selector/#comments</comments>
		<pubDate>Mon, 08 Apr 2013 23:30:51 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-use-a-selector/</guid>
		<description><![CDATA[Just a quick reminder before I forget (again) how selectors are called: Some of Apple&#8217;s pre-written methods use whats known as a Selector. Here&#8217;s an example method that uses a selector: // create a bar button item UIBarButtonItem *linkButton = [[UIBarButtonItem alloc]initWithTitle:@"My Title" style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod)]; Notice how the selector is created on the second [...]]]></description>
				<content:encoded><![CDATA[<p>Just a quick reminder before I forget (again) how selectors are called:</p>
<p>Some of Apple&#8217;s pre-written methods use whats known as a Selector. Here&#8217;s an example method that uses a selector:</p>
<pre class="wp-code-highlight prettyprint">
// create a bar button item
UIBarButtonItem *linkButton = [[UIBarButtonItem alloc]initWithTitle:@&quot;My Title&quot; style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod)];
</pre>
<p>Notice how the selector is created on the second line. All it wants to know here is the method name really. Imagine you had a method called myMethod in the current class (hence the use of self here), this is how it gets called via the action parameter.</p>
<p>If you were to call this method just by itself, you&#8217;d use the usual</p>
<pre class="wp-code-highlight prettyprint">
[self myMethod];
</pre>
<p>For more information and how to build your own selectors, check out the <a href="http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Selector.html%23//apple_ref/doc/uid/TP40008195-CH48-SW1">Cocoa Core Competencies section</a> on Selectors.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/ClL_DCJ84zw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-use-a-selector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-use-a-selector/</feedburner:origLink></item>
		<item>
		<title>How to remove the first n characters from an NSString</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/nuTOQM_0NM8/</link>
		<comments>http://wpguru.co.uk/2013/04/how-to-remove-the-first-n-characters-from-an-nsstring/#comments</comments>
		<pubDate>Fri, 05 Apr 2013 07:01:56 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-remove-the-first-n-characters-from-an-nsstring/</guid>
		<description><![CDATA[There&#8217;s a convenient method for that by the name of substringFromIndex. This example removes the first character: NSString *complete = @"12345"; NSString *cutOff = [complete substringFromIndex:1]; NSLog(@"%@", cutOff); // cutOff is now 2345 substringFromString takes only one parameter which specifies where to start the new string. The first character is 0 based, like in arrays. [...]]]></description>
				<content:encoded><![CDATA[<p>There&#8217;s a convenient method for that by the name of substringFromIndex. This example removes the first character:</p>
<pre class="wp-code-highlight prettyprint">
    NSString *complete = @&quot;12345&quot;;
    NSString *cutOff = [complete substringFromIndex:1];
    NSLog(@&quot;%@&quot;, cutOff);
    // cutOff is now 2345
</pre>
<p>substringFromString takes only one parameter which specifies where to start the new string. The first character is 0 based, like in arrays. The parameter can&#8217;t be longer than the original string&#8217;s length for obvious reasons.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/nuTOQM_0NM8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/04/how-to-remove-the-first-n-characters-from-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/04/how-to-remove-the-first-n-characters-from-an-nsstring/</feedburner:origLink></item>
		<item>
		<title>How to create a Fetch Request in the Xcode Model Editor</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/SvIlK01CV8g/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-a-fetch-request-in-the-xcode-model-editor/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 22:48:21 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-a-fetch-request-in-the-xcode-model-editor/</guid>
		<description><![CDATA[You can create Fetch Requests in the Xcode model editor, including a Predicate. These are somewhat easier to setup. To create one, select your .xcdatamodeld file, then head over to Editor &#8211; Add Fetch Request. Give it a catchy name and a filter property, and much of the above code becomes obsolete. Here&#8217;s how you [...]]]></description>
				<content:encoded><![CDATA[<p>You can create Fetch Requests in the Xcode model editor, including a Predicate. These are somewhat easier to setup. To create one, select your .xcdatamodeld file, then head over to Editor &#8211; Add Fetch Request. Give it a catchy name and a filter property, and much of the above code becomes obsolete.</p>
<p>Here&#8217;s how you can retrieve one of those in code:</p>
<pre class="wp-code-highlight prettyprint">
    // create a fetch request from template
    NSFetchRequest *fetchRequest = [self.managedObjectModel fetchRequestTemplateForName:@&quot;MyFetch&quot;];
    
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    for (Event *event in fetchedObjects) {
        NSLog(@&quot;%@&quot;, [event.timeStamp description]);
    }
</pre>
<p>Note that to retrieve your Fetch Request Template you need a reference to your Managed Object Model as well as your context. The easiest way to grab hold of it in an iOS app is to pass it in on a property, just like we did with self.managedoObjectContext.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/SvIlK01CV8g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-a-fetch-request-in-the-xcode-model-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-a-fetch-request-in-the-xcode-model-editor/</feedburner:origLink></item>
		<item>
		<title>How to retrieve a Managed Object in Core Data Fetch Requests</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/MgHH57GAN88/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-retrieve-a-managed-object-in-core-data-fetch-requests/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 18:19:54 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-retrieve-a-managed-object-in-core-data-using-basic-fetch-requests/</guid>
		<description><![CDATA[Retrieving Managed Objects is somewhat more complex than creating them, mainly because you can filter what you&#8217;re getting back rather than retrieve everything that your store file has to offer. Let&#8217;s first illustrate a basic NSFetchRequest. For the following examples I&#8217;m using the iOS Master/Detail template which provides an Entity called Event with a property [...]]]></description>
				<content:encoded><![CDATA[<p>Retrieving Managed Objects is somewhat more complex than creating them, mainly because you can filter what you&#8217;re getting back rather than retrieve everything that your store file has to offer.</p>
<p>Let&#8217;s first illustrate a basic NSFetchRequest. For the following examples I&#8217;m using the iOS Master/Detail template which provides an Entity called Event with a property called timeStamp. I&#8217;ve created custom subclasses for this entity. Press the add button a few times so we have some data, then quit the application.</p>
<h3>Basic Fetch Request</h3>
<p>Here&#8217;s how we can retrieve all our values using a basic Fetch Request:</p>
<pre class="wp-code-highlight prettyprint">    // create a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@&quot;Event&quot; inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // fetch all objects
    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;amp;error];
    if (fetchedObjects == nil) {
        NSLog(@&quot;Houston, we have a problem: %@&quot;, error);
    }

    // display all objects
    for (Event *event in fetchedObjects) {
        NSLog(@&quot;%@&quot;, [event.timeStamp description]);
    }</pre>
<p>The easiest way to get this code is by using a code snippet from the Xcode library (search for fetch, the first one is a &#8220;basic fetch&#8221;).</p>
<p>Note that the results are unfiltered, which means they&#8217;re not necessarily in the order you would like them to be in. To remedy this, you can use a Sort Descriptor:</p>
<h3>Fetch Request with Sort Descriptors</h3>
<pre class="wp-code-highlight prettyprint">    // create a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@&quot;Event&quot; inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // define a sort descriptor
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:@&quot;timeStamp&quot; ascending:YES];
    NSArray *scArray = [[NSArray alloc]initWithObjects:descriptor, nil];

    // give sort descriptor array to the fetch request
    fetchRequest.sortDescriptors = scArray;

    // fetch all objects
    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;amp;error];
    if (fetchedObjects == nil) {
        NSLog(@&quot;Houston, we have a problem: %@&quot;, error);
    }

    // display all objects
    for (Event *event in fetchedObjects) {
        NSLog(@&quot;%@&quot;, [event.timeStamp description]);
    }</pre>
<p>You can pass more than one Sort Descriptor, hence we need to pass an NSArray to the fetch request. A usage example would be &#8220;list all employers alphabetically, in order of which they&#8217;ve started working&#8221;. To do this, create another sort descriptor, then add both to the array.</p>
<p>The easiest way to get this is to use the Xcode Code Snippets again, this time select the &#8220;fetch with sorting&#8221; and it will have all this code ready for you.</p>
<h3>Fetch Request using a Predicate</h3>
<p>Predicates are filters with which you can specify conditions, such as &#8220;is this date older than x&#8221;, &#8220;does the first name contain steve&#8221;, and so forth. I have written a more in-depth article on Predictates elsewhere on this site and won&#8217;t go into the details.</p>
<p>Here&#8217;s how you create a Fetch Request with a Predicate, combined with a Sort Descriptor:</p>
<pre class="wp-code-highlight prettyprint">    // create a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@&quot;Event&quot; inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    // setup a predicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@&quot;timeStamp &amp;lt; %@&quot;, [NSDate date]];
    
    // give the predicate to the fetch request
    fetchRequest.predicate = predicate;
    
    // define a sort descriptor
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:@&quot;timeStamp&quot; ascending:YES];
    NSArray *scArray = [[NSArray alloc]initWithObjects:descriptor, nil];
    
    // give sort descriptor array to the fetch request
    fetchRequest.sortDescriptors = scArray;
    
    // fetch all objects
    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;amp;error];
    if (fetchedObjects == nil) {
        NSLog(@&quot;Houston, we have a problem: %@&quot;, error);
    }
    
    // display all objects
    for (Event *event in fetchedObjects) {
        NSLog(@&quot;%@&quot;, [event.timeStamp description]);
    }</pre>
<p>&nbsp;</p>
<p>If you don&#8217;t need the Sort Descriptor, just remove that code from the example. This snippet is also available from Xcode and is called &#8220;fetch with predicate&#8221;.</p>
<p>For more information on Predicates, check out <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/predicates.html" >Apple&#8217;s Predicate Programming Guide</a>.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/MgHH57GAN88" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-retrieve-a-managed-object-in-core-data-fetch-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-retrieve-a-managed-object-in-core-data-fetch-requests/</feedburner:origLink></item>
		<item>
		<title>How to create a Managed Object in Core Data</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/BSNXTVl8bEg/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-a-managed-object-in-core-data/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 17:43:55 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-a-managed-object-in-core-data/</guid>
		<description><![CDATA[Assuming you&#8217;re using an app template that includes Core Data, you will have access to the Managed Object Context. In the simplest form, and without custom Entity classes setup, you can use key/value coding to set your object&#8217;s properties. In fact, the Master/Detail template does this. Here&#8217;s an example for an Entity named Event with [...]]]></description>
				<content:encoded><![CDATA[<p>Assuming you&#8217;re using an app template that includes Core Data, you will have access to the Managed Object Context. In the simplest form, and without custom Entity classes setup, you can use key/value coding to set your object&#8217;s properties. In fact, the Master/Detail template does this.</p>
<p>Here&#8217;s an example for an Entity named Event with two properties (myKey and anotherKey):</p>
<pre class="wp-code-highlight prettyprint">
// create a managed object
NSManagedObject *myObject = [NSEntityDescription insertNewObjectForEntityForName:@&quot;Event&quot; inManagedObjectContext:self.managedObjectContext];
    
// set its attributes
[myObject setValue:@&quot;This is my value&quot; forKey:@&quot;myKey&quot;];
[myObject setValue:@&quot;My other value&quot; forKey:@&quot;anotherKey&quot;];
</pre>
<p>If you have created custom NSManagedObject subclasses for your Entity then you can create an object like this:</p>
<pre class="wp-code-highlight prettyprint">
// create a managed object from my custom class
Event *myObject = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@&quot;Event&quot; inManagedObjectContext:self.managedObjectContext];
    
// set my object&#039;s properties
myObject.myKey = @&quot;This is my value&quot;;
myObject.anotherKey = @&quot;My other value&quot;;
</pre>
<p>The KVC method will always work, regardless if you have custom classes or not. Custom Classes have the advantage that you can add custom behaviour (i.e. methods), and of course code completion in Xcode.</p>
<p>Note that these snippets will create an object, but it your values are only stored once you save the Managed Object Context:</p>
<pre class="wp-code-highlight prettyprint">
[self.managedObjectContext save:nil];
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/BSNXTVl8bEg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-a-managed-object-in-core-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-a-managed-object-in-core-data/</feedburner:origLink></item>
		<item>
		<title>How to create a Save As dialogue with NSSavePanel</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/5PmxY0r_Pqg/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-a-save-as-dialogue-with-nssavepanel/#comments</comments>
		<pubDate>Thu, 21 Mar 2013 18:30:49 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-a-save-as-dialogue-with-nssavepanel/</guid>
		<description><![CDATA[Likewise we can save our previously selected file using an NSSavePanel. It too is easy to use, just as the NSOpenPanel. For a save action to make sense we need to have some data to save, so in this example we will copy an existing file (self.myURL) to the new URL that the save panel [...]]]></description>
				<content:encoded><![CDATA[<p>Likewise we can save our previously selected file using an NSSavePanel. It too is easy to use, just as the NSOpenPanel. </p>
<p>For a save action to make sense we need to have some data to save, so in this example we will copy an existing file (self.myURL) to the new URL that the save panel returns. We&#8217;ll let the NSFileManager just create a copy with a new name that the user specifies using the save panel:</p>
<pre class="wp-code-highlight prettyprint">
- (IBAction)saveFile:(id)sender {
    
    // create the save panel
    NSSavePanel *panel = [NSSavePanel savePanel];
    
    // set a new file name
    [panel setNameFieldStringValue:@&quot;NewFile.png&quot;];
    
    // display the panel
    [panel beginWithCompletionHandler:^(NSInteger result) {
        
        if (result == NSFileHandlingPanelOKButton) {
            
            // create a file namaner and grab the save panel&#039;s returned URL
            NSFileManager *manager = [NSFileManager defaultManager];
            NSURL *saveURL = [panel URL];
            
            // then copy a previous file to the new location
            [manager copyItemAtURL:self.myURL toURL:saveURL error:nil];
        }
    }];
    
}
</pre>
<p>The only alien looking thing here is the use of a Block (like it is in the NSOpenPanel). This is the equivalent to an anonymous function in JavaScript, basically a block of code that runs upon completion. As it&#8217;s part of the method call, the closing ] is way at the bottom.</p>
<h3>Further Reading</h3>
<p>Apple&#8217;s NSSavePanel Class Reference:</p>
<ul>
<li><a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSavePanel_Class/Reference/Reference.html" rel="nofollow">https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSavePanel_Class/Reference/Reference.html</a></li>
</ul>
<p>A Short Practical Guide to Blocks:</p>
<ul>
<li><a href="https://developer.apple.com/library/mac/#featuredarticles/Short_Practical_Guide_Blocks/" rel="nofollow">https://developer.apple.com/library/mac/#featuredarticles/Short_Practical_Guide_Blocks/</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/5PmxY0r_Pqg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-a-save-as-dialogue-with-nssavepanel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-a-save-as-dialogue-with-nssavepanel/</feedburner:origLink></item>
		<item>
		<title>How to create an Open File dialogue with NSOpenPanel</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/0uoARtHYbbk/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-an-open-file-dialogue-with-nsopenpanel/#comments</comments>
		<pubDate>Thu, 21 Mar 2013 18:19:05 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-an-open-file-dialogue-with-nsopenpanel/</guid>
		<description><![CDATA[NSOpenPanel is surprisingly easy to use: create the panel, call the openPanel method, and handle the returned URL object in a block. In this example we invoke the panel from a button in the main window, then display the returned URL in a textLabel: - (IBAction)loadFile:(id)sender { // create an open documet panel NSOpenPanel *panel [...]]]></description>
				<content:encoded><![CDATA[<p>NSOpenPanel is surprisingly easy to use: create the panel, call the openPanel method, and handle the returned URL object in a block. </p>
<p>In this example we invoke the panel from a button in the main window, then display the returned URL in a textLabel:</p>
<pre class="wp-code-highlight prettyprint">
- (IBAction)loadFile:(id)sender {
    
    // create an open documet panel
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    
    // display the panel
    [panel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            
            // grab a reference to what has been selected
            NSURL *theDocument = [[panel URLs]objectAtIndex:0];
            
            // write our file name to a label
            NSString *theString = [NSString stringWithFormat:@&quot;%@&quot;, theDocument];
            self.textLabel.stringValue = theString;
            
        }
    }];
    
}
</pre>
<h3>Further Reading</h3>
<p>Apple&#8217;s NSOpenPanel Class reference:</p>
<ul>
<li><a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSOpenPanel_Class/Reference/Reference.html" rel="nofollow">https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSOpenPanel_Class/Reference/Reference.html</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/0uoARtHYbbk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-an-open-file-dialogue-with-nsopenpanel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-an-open-file-dialogue-with-nsopenpanel/</feedburner:origLink></item>
		<item>
		<title>How to pass data from the App Delegate to your Top View Controller</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/91dQMVBV4XI/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-pass-data-from-the-app-delegate-to-your-top-view-controller/#comments</comments>
		<pubDate>Wed, 20 Mar 2013 06:10:30 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-pass-data-from-the-app-delegate-to-your-top-view-controller/</guid>
		<description><![CDATA[If the App Delegate has something that your top view controller needs then you simply pass it the required object via a property set on the top view controller. This is easy when your top view controller is also the root view controller. However, when you embed your top view controller in a Navigation Controller, [...]]]></description>
				<content:encoded><![CDATA[<p>If the App Delegate has something that your top view controller needs then you simply pass it the required object via a property set on the top view controller. This is easy when your top view controller is also the root view controller.</p>
<p>However, when you embed your top view controller in a Navigation Controller, and perhaps that one is also embedded in a Tab Bar Controller, then this array isn&#8217;t quite so easy to figure out (and I must admit that I always forget how to do this when a new challenge arises).</p>
<p>So here&#8217;s how this works. In this example, the thing that&#8217;s displayed is called MyViewController, which is embedded in a Navigation Controller. I&#8217;m passing it the App Delegate&#8217;s self.managedObjectContext which is defined as a property on MyViewController.</p>
<pre class="wp-code-highlight prettyprint">
// grab our own navigation controller
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    
// grab our table view controller
MyViewController *myViewController = (MyViewController *)navigationController.topViewController;
    
// pass in whatever data you need (such as this):
myViewController.managedObjectContext = self.managedObjectContext;
</pre>
<p>We call this in the applicationDidFinishLaunching method, just before the return statement.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/91dQMVBV4XI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-pass-data-from-the-app-delegate-to-your-top-view-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-pass-data-from-the-app-delegate-to-your-top-view-controller/</feedburner:origLink></item>
		<item>
		<title>How to return the number of rows in a UITableView with Core Data</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/8701obcRZZg/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-return-the-number-of-rows-in-a-uitableview-with-core-data/#comments</comments>
		<pubDate>Mon, 18 Mar 2013 15:38:41 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[UITableViewCell]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-return-the-number-of-rows-in-a-uitableview-with-core-data/</guid>
		<description><![CDATA[Likewise, here&#8217;s how we return the number of rows using a fetched results controller &#8211; as provided by the Master / Detail template: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; }]]></description>
				<content:encoded><![CDATA[<p>Likewise, here&#8217;s how we return the number of rows using a fetched results controller &#8211; as provided by the Master / Detail template:</p>
<pre class="wp-code-highlight prettyprint">
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/8701obcRZZg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-return-the-number-of-rows-in-a-uitableview-with-core-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-return-the-number-of-rows-in-a-uitableview-with-core-data/</feedburner:origLink></item>
		<item>
		<title>How to return the number of sections in a UITableView with Core Data</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/n-HkIVoScbE/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-return-the-number-of-sections-in-a-uitableview-with-core-data/#comments</comments>
		<pubDate>Mon, 18 Mar 2013 15:36:15 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-return-the-number-of-sections-in-a-uitableview-with-core-data/</guid>
		<description><![CDATA[I can never remember how to do this. Here&#8217;s the method provided by the Master / Detail template when using Core Data: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id &#60;NSFetchedResultsSectionInfo&#62; sectionInfo = [self.fetchedResultsController sections][section]; return [sectionInfo numberOfObjects]; }]]></description>
				<content:encoded><![CDATA[<p>I can never remember how to do this. Here&#8217;s the method provided by the Master / Detail template when using Core Data:</p>
<pre class="wp-code-highlight prettyprint">
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
}
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/n-HkIVoScbE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-return-the-number-of-sections-in-a-uitableview-with-core-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-return-the-number-of-sections-in-a-uitableview-with-core-data/</feedburner:origLink></item>
		<item>
		<title>How to convert a Persistent Store from XML to SQLite in Core Data</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/7pWqh4EWuxQ/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-convert-a-persistent-store-from-xml-to-sqlite-in-core-data/#comments</comments>
		<pubDate>Sun, 17 Mar 2013 23:11:10 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-convert-a-persistent-store-from-xml-to-sqlite-in-core-data/</guid>
		<description><![CDATA[By default Mac Apps use XML as store type when saving in Core Data, even though it can use other formats &#8211; such as SQLite, which is the default on iOS. Here&#8217;s a way to quickly convert one into the other. Note that this is designed as a one-off operation to create a new store [...]]]></description>
				<content:encoded><![CDATA[<p>By default Mac Apps use XML as store type when saving in Core Data, even though it can use other formats &#8211; such as SQLite, which is the default on iOS. Here&#8217;s a way to quickly convert one into the other.</p>
<p>Note that this is designed as a one-off operation to create a new store file which can be used in another application. Once run, comment the code out and either change your store file, or take the store file and use it in another app.</p>
<p>Add this code to the AppDelegate.m file in the persistenStore method (just before the return statement):</p>
<pre class="wp-code-highlight prettyprint">
// convert this thing to SQL
NSURL *sqlstore = [applicationFilesDirectory URLByAppendingPathComponent:@&quot;NewStoreFile.sqldata&quot;];
NSPersistentStore *xmlstore = [coordinator persistentStoreForURL:url];
    
[coordinator migratePersistentStore:xmlstore toURL:sqlstore options:nil withType:NSSQLiteStoreType error:nil];
</pre>
<p>For more information on how to do this in context, check out Simon Allardice&#8217;s superb course <a href="http://www.lynda.com/iOS-tutorials/Core-Data-iOS-OS-X/101461-2.html?utm_medium=affiliate&#038;utm_source=ldc_affiliate&%23038;utm_content=524&%23038;utm_campaign=CD14694&%23038;bid=524&%23038;aid=CD14694&%23038;opt=" >Core Data for iOS and OS X</a> on Lynda.com.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/7pWqh4EWuxQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-convert-a-persistent-store-from-xml-to-sqlite-in-core-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-convert-a-persistent-store-from-xml-to-sqlite-in-core-data/</feedburner:origLink></item>
		<item>
		<title>Binding an NSTableView to Core Data without code</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/A6oJVexKHKA/</link>
		<comments>http://wpguru.co.uk/2013/03/binding-an-nstableview-to-core-data-without-code/#comments</comments>
		<pubDate>Sun, 17 Mar 2013 20:09:49 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/binding-an-nstableview-to-core-data-without-code/</guid>
		<description><![CDATA[I was excited to find out that it is possible to write a Mac App with Core Data completely without code! This magic is possible with something that&#8217;s not available in iOS (yet) called Cocoa Bindings. You provide the user controls you need in Xcode, then control-drag your way to extensive functionality and happiness. Before [...]]]></description>
				<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-715" alt="Mountain Lion Logo" src="http://pinkstone.co.uk/wp-content/uploads/2013/03/mountain_lion_hero-150x150.jpg" width="150" height="150" />I was excited to find out that it is possible to write a Mac App with Core Data completely without code! This magic is possible with something that&#8217;s not available in iOS (yet) called Cocoa Bindings.</p>
<p>You provide the user controls you need in Xcode, then control-drag your way to extensive functionality and happiness. Before I forget, I thought I&#8217;d better make some notes.</p>
<p>Here&#8217;s a step-by-step list:</p>
<ul>
<li>Using Xcode, create a new Mac Cocoa Application with Core Data</li>
<li>Setup your entities and properties in your Core Data Model (.xcdatamodeld)</li>
<li>Create relationships between Entities if desired</li>
<li>Select all Entities and create custom classes from them (using Editor &#8211; create NSManagedObject Subclasses)</li>
</ul>
<p>Drag our friend the Array Controller onto the Object&#8217;s Bar. If you have multiple Entities, you need a separate Array Controller for each one. Change the labels to tell them apart (select a different file and go back to the xib in order for those labels to be visible &#8211; it&#8217;s a bug in Xcode).</p>
<p>Now bind the Array Controller and Core Data Managed Object Context together:</p>
<ul>
<li>Select your Array Controller, then go to the Bindings Inspector (second from the right)</li>
<li>Under Parameters, head over to the Managed Object Context and bind to the App Delegate (as that&#8217;s where Xcode has kindly prepared our Core Data stack)</li>
<li>In Model Key Path, type self.managedObjectContext (this should self complete)</li>
<li>In the Attributes Inspector, tell the controller that we&#8217;re using an Entity instead of a Class, then enter the Entity (it&#8217;s the one you&#8217;ve created in the data model)</li>
<li>While you&#8217;re here, tick the &#8220;Prepares Content&#8221; check box if you want the table view to be populated automatically</li>
</ul>
<p>Add a Table View to your window and bind it to the Array Controller:</p>
<ul>
<li>Select the Table View (inside the Scroll View) and head over to the Bindings Inspector</li>
<li>Under Table Content, select Bind To your Array Controller</li>
<li>Now select the actual Table Column you want to fill with data, and still inside the Bindings Inspector under Value, bind the column to your Array Controller</li>
<li>In the field for Model Key Path, type the attribute/property you&#8217;d like to see in this column (you&#8217;ve defined this in the data model)</li>
<li>Repeat the process for each column you want to fill with data</li>
</ul>
<p>To add data to your table:</p>
<ul>
<li>Add a couple of Gradient Buttons to your window</li>
<li>Make one the &#8220;Add&#8221; and one the &#8220;Delete&#8221; button</li>
<li>Hook up each button to the Array Controller via control drag, selecting add and remove respectively</li>
</ul>
<p>Congratulations: You&#8217;ve just created an editable Table View, hooked up to Core Data without a single line of code!</p>
<p>Let&#8217;s create a functional SAVE menu item</p>
<p>If you quit the app (rather than running it again from Xcode), all your data will save. However we can implement a manual save action with ease. The menu item itself is there by default, all we need to do is hook it up to the App Delegate and bind it to the save function.</p>
<p>In the Object Sidebar, select Main Menu, then select File &#8211; Save (which will expand the item in the sidebar too). From the save menu option, control drag to the App Delegate and select saveAction.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/A6oJVexKHKA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/binding-an-nstableview-to-core-data-without-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/binding-an-nstableview-to-core-data-without-code/</feedburner:origLink></item>
		<item>
		<title>How to assure your values are as intended with NSAssert</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/wI1te7HwvW4/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-assure-your-values-are-as-intended-with-nsassert/#comments</comments>
		<pubDate>Sun, 17 Mar 2013 03:55:40 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-assure-your-values-are-as-intended-with-nsassert/</guid>
		<description><![CDATA[NSAssert is a macro which allows you to test for specific values if and when they occur. Rather than having to figure out where your app has passed the wrong value several stages before a problem happened, NSAssert can be used like NSLog &#8211; with the helpful difference that if all is well, there&#8217;s no [...]]]></description>
				<content:encoded><![CDATA[<p>NSAssert is a macro which allows you to test for specific values if and when they occur. Rather than having to figure out where your app has passed the wrong value several stages before a problem happened, NSAssert can be used like NSLog &#8211; with the helpful difference that if all is well, there&#8217;s no log output.</p>
<p>Imagine your app is calculating a value. We can check if the value is as we had expected it to be:</p>
<pre class="wp-code-highlight prettyprint">
    // NSAssert Example (nothing happens)
    int i = 47;
    NSAssert(i == 47, @&quot;That didn&#039;t work&quot;);
</pre>
<p>In this case, everything is fine and we don&#8217;t see any log output. Let&#8217;s change the value to something else, and see our message together with a rather intimidating stack trace hit our log:</p>
<pre class="wp-code-highlight prettyprint">
    // NSAssert Example (will cause an exception)
    int i = 48;
    NSAssert(i == 47, @&quot;That didn&#039;t work&quot;);
</pre>
<p>You can also pass values and print them to the log. Depending on how many variables you pass, add a number to NSAssert like so:</p>
<pre class="wp-code-highlight prettyprint">
    // NSAssert Example (pass one value)
    int i = 48;
    NSAssert1(i == 47, @&quot;The real value is %i&quot;, i);
</pre>
<p>Use NSAssert2, NSAssert3 and so forth for more than one value.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/wI1te7HwvW4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-assure-your-values-are-as-intended-with-nsassert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-assure-your-values-are-as-intended-with-nsassert/</feedburner:origLink></item>
		<item>
		<title>What is an Exception Breakpoint</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/D3hgUATtYq8/</link>
		<comments>http://wpguru.co.uk/2013/03/what-is-an-exception-breakpoint/#comments</comments>
		<pubDate>Sun, 17 Mar 2013 03:11:02 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/what-is-an-exception-breakpoint/</guid>
		<description><![CDATA[Sometimes Xcode just throws an error message that isn&#8217;t very helpful. That&#8217;s usually when we have to try and figure out why something didn&#8217;t go the way we expected it. Other times however, Xcode tells us very clearly which element it isn&#8217;t happy with &#8211; we fix it and our app progresses. Why can&#8217;t it [...]]]></description>
				<content:encoded><![CDATA[<p>Sometimes Xcode just throws an error message that isn&#8217;t very helpful. That&#8217;s usually when we have to try and figure out why something didn&#8217;t go the way we expected it. Other times however, Xcode tells us very clearly which element it isn&#8217;t happy with &#8211; we fix it and our app progresses. Why can&#8217;t it always be that way?</p>
<p>Well that&#8217;s because at those unhelpful times, Xcode is technically picking up an &#8220;exception&#8221;, but it tries to continue to run the programme. That&#8217;s when we end up with an error message which isn&#8217;t related to the actual problem.</p>
<p>An Exception Breakpoint can help. It&#8217;s something we can set and therefore force Xcode to stop when the problem occurs, not several lines later. Here&#8217;s how we do it:</p>
<ul>
<li>with the Navigator pane open, select the 6th tab from the left (the Breakpoint Navigator)</li>
<li>at the bottom left, click the plus sign to add a breakpoint</li>
<li>from the context menu select Add Exception Breakpoint</li>
<li>leave all default values and hit Done</li>
</ul>
<p>Next time Xcode will not try to be heroic and keep going. Instead, expect a more helpful error message.</p>
<p><img src="http://pinkstone.co.uk/wp-content/uploads/2013/03/Screen-Shot-2013-03-16-at-23.03.17.png" alt="Screen Shot 2013-03-16 at 23.03.17" width="481" height="315" class="aligncenter size-full wp-image-705" /></p>
<p>Also check out: <a href="https://developer.apple.com/library/ios/#recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html" rel="nofollow">https://developer.apple.com/library/ios/#recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html</a></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/D3hgUATtYq8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/what-is-an-exception-breakpoint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/what-is-an-exception-breakpoint/</feedburner:origLink></item>
		<item>
		<title>How to activate a UISearchDisplayController</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/7ejYXgGh9Ts/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-activate-a-uisearchdisplaycontroller/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 16:39:00 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-activate-a-uisearchdisplaycontroller/</guid>
		<description><![CDATA[We can do this two ways: // version 1: [self.searchBar becomeFirstResponder]; // version 2: [self.searchDisplayController setActive:YES animated:YES]; The benefit of version 2 is that if we perform it &#8220;in vision&#8221;, we can use the animated option and make it look all swish. Version 2 of course also has a reverse with which we can deactivate [...]]]></description>
				<content:encoded><![CDATA[<p>We can do this two ways:</p>
<pre class="wp-code-highlight prettyprint">
    // version 1:
    [self.searchBar becomeFirstResponder];

    // version 2:
    [self.searchDisplayController setActive:YES animated:YES];
</pre>
<p>The benefit of version 2 is that if we perform it &#8220;in vision&#8221;, we can use the animated option and make it look all swish. </p>
<p>Version 2 of course also has a reverse with which we can deactivate the controller:</p>
<pre class="wp-code-highlight prettyprint">
    [self.searchDisplayController setActive:NO animated:YES];
</pre>
<p>Note that this will not affect the UISearchBar associated with the search display controller; you need to deal with that separately.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/7ejYXgGh9Ts" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-activate-a-uisearchdisplaycontroller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-activate-a-uisearchdisplaycontroller/</feedburner:origLink></item>
		<item>
		<title>How to remove the last n characters from an NSString</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/qvcpjdeMpm8/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-extract-the-last-n-characters-from-an-nsstring/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 05:55:21 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-remove-the-last-n-characters-from-an-nsstring/</guid>
		<description><![CDATA[We can use the substringToIndex method for this, using the length of our original string minus 1: NSString *something = @"12345"; something = [something substringToIndex:(something.length - 1)]; // something is now 1234]]></description>
				<content:encoded><![CDATA[<p>We can use the substringToIndex method for this, using the length of our original string minus 1:</p>
<pre class="wp-code-highlight prettyprint">
        NSString *something = @&quot;12345&quot;;
        something = [something substringToIndex:(something.length - 1)];
        
        // something is now 1234
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/qvcpjdeMpm8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-extract-the-last-n-characters-from-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-extract-the-last-n-characters-from-an-nsstring/</feedburner:origLink></item>
		<item>
		<title>How to extract first n characters of an NSString</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/2I0uHm4W0TI/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-extract-first-n-characters-of-an-nsstring/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 06:02:34 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-extract-a-portion-of-an-nsstring/</guid>
		<description><![CDATA[We can use substringWithRange for this, which requires us to define a range: NSString *something = @"123456789"; NSRange myRange = NSMakeRange(2, 3); something = [something substringWithRange:myRange]; // something is now 345 You create a range by specifying where the substring should start (the first character is 0, like in an array), then specify how many [...]]]></description>
				<content:encoded><![CDATA[<p>We can use substringWithRange for this, which requires us to define a range:</p>
<pre class="wp-code-highlight prettyprint">
        NSString *something = @&quot;123456789&quot;;
        NSRange myRange = NSMakeRange(2, 3);
        something = [something substringWithRange:myRange];

        // something is now 345
</pre>
<p>You create a range by specifying where the substring should start (the first character is 0, like in an array), then specify how many characters from there you want to use.</p>
<p>To grab just the first character you&#8217;d specify a range like this:</p>
<pre class="wp-code-highlight prettyprint">
        NSString *something = @&quot;123456789&quot;;
        NSRange myRange = NSMakeRange(0, 1);
        something = [something substringWithRange:myRange];

        // something is now 1
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/2I0uHm4W0TI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-extract-first-n-characters-of-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-extract-first-n-characters-of-an-nsstring/</feedburner:origLink></item>
		<item>
		<title>How to create your own Data Type using typedef</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/gsCsH20Qn8c/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-your-own-data-type-using-typedef/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 15:02:38 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[typedef]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-your-own-data-type-using-typedef/</guid>
		<description><![CDATA[With typedef you can specify custom variable types. Instead of having a variable such as int, you could create your own &#8211; such as yourInt: // create a typedef of int typedef int yourInt; yourInt yourValue = 57; NSLog(@"Your value is %i", yourValue); Now you have your own variable type called youInt which behaves just [...]]]></description>
				<content:encoded><![CDATA[<p>With typedef you can specify custom variable types. Instead of having a variable such as int, you could create your own &#8211; such as yourInt:</p>
<pre class="wp-code-highlight prettyprint">
// create a typedef of int
        typedef int yourInt;
        yourInt yourValue = 57;
        
        NSLog(@&quot;Your value is %i&quot;, yourValue);
</pre>
<p>Now you have your own variable type called youInt which behaves just like an int. </p>
<p>The power of typedef becomes clearer when combined with enumerations we were discussing previously. You can create your own data type of enum, creating and addressing your values more easily. Let&#8217;s stick with the drink size example from earlier:</p>
<pre class="wp-code-highlight prettyprint">
// create a typedef of enum drinkSize
        typedef enum drinkSize {
            smallDrink = 1,
            mediumDrink = 50,
            largeDrink = 100
        } drinkSize;
        
        // declare a variable from my typedef enumeration set
        drinkSize myDrink = largeDrink;
        drinkSize juliasDrink = smallDrink;

</pre>
<p>Xcode will code complete all available values, making your life a bit easier when coding. </p>
<p>Many of Apple&#8217;s pre-defined super long complex types are created this way. Think of all those NSDateFormatter values you can use, such as NSDateFormatterStyleLongStyle.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/gsCsH20Qn8c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-your-own-data-type-using-typedef/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-your-own-data-type-using-typedef/</feedburner:origLink></item>
		<item>
		<title>How to create an Enumeration (enum)</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/oGOT0ciAoSk/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-an-enumeration-enum/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 14:43:30 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-an-enumeration-enum/</guid>
		<description><![CDATA[An Enumeration (enum) allows you to create a collection of custom values. These can be more meaningful than simply using numbers. Imagine you want to track drink sizes, such as small, medium and large. You can remember to just use the numbers 1, 2 and 3 for each size &#8211; but Enumeration makes it easier [...]]]></description>
				<content:encoded><![CDATA[<p>An Enumeration (enum) allows you to create a collection of custom values. These can be more meaningful than simply using numbers. </p>
<p>Imagine you want to track drink sizes, such as small, medium and large. You can remember to just use the numbers 1, 2 and 3 for each size &#8211; but Enumeration makes it easier to declare those.</p>
<pre class="wp-code-highlight prettyprint">
// create an enumeration
        enum drinkSize {
            smallDrink = 1,
            mediumDrink = 50,
            largeDrink = 100
        };
        
        // declare a variable from my enumeration set
        enum drinkSize myDrink = largeDrink;
        
        // compare against my own values
        if (myDrink == largeDrink) {
            NSLog(@&quot;That&#039;s my drink!&quot;);
        }
        
        // an enum behaves like an integer
        NSLog(@&quot;My Drink Size is %i&quot;, myDrink);
</pre>
<p>Notice the last line: when written in a string like a log message, Objective-C will track your enumeration&#8217;s numeric values instead. In my case, the log message would display &#8220;My Drink Size is 100&#8243;.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/oGOT0ciAoSk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-an-enumeration-enum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-an-enumeration-enum/</feedburner:origLink></item>
		<item>
		<title>How to create an NSIndexPath and specify its components</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/BQbryyoYvd0/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-create-an-nsindexpath-and-specify-its-components/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 04:27:32 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSIndexPath]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-an-nsindexpath-and-specify-its-components/</guid>
		<description><![CDATA[On occasion we need to create an NSIndexPath manually, with components we specify (such as a row or a section). There&#8217;s a method for that: indexPath:forItem:inSection. Here&#8217;s how you create an indexPath for row 0, section 0: NSIndexPath *indexPath = [[NSIndexPath alloc]init]; indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; The method takes two integers. You can even [...]]]></description>
				<content:encoded><![CDATA[<p>On occasion we need to create an NSIndexPath manually, with components we specify (such as a row or a section). There&#8217;s a method for that: indexPath:forItem:inSection.</p>
<p>Here&#8217;s how you create an indexPath for row 0, section 0:</p>
<pre class="wp-code-highlight prettyprint">
NSIndexPath *indexPath = [[NSIndexPath alloc]init];
indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
</pre>
<p>The method takes two integers. You can even take an existing indexPath, then add or subtract values to the new indexPath, like so:</p>
<pre class="wp-code-highlight prettyprint">
// current indexPath
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

// make a new indexPath and add 1 to the row of the previous one
NSIndexPath *indexPath2 = [[NSIndexPath alloc]init];
indexPath2 = [NSIndexPath indexPathForItem:(indexPath.row + 1) inSection:indexPath.section];
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/BQbryyoYvd0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-create-an-nsindexpath-and-specify-its-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-create-an-nsindexpath-and-specify-its-components/</feedburner:origLink></item>
		<item>
		<title>Hilarious Xcode Error Message</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/R07r2yfoD68/</link>
		<comments>http://wpguru.co.uk/2013/03/hilarious-xcode-error-message/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 04:53:01 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://wpguru.co.uk/?p=3081</guid>
		<description><![CDATA[I&#8217;ve been working on an iOS App recently which deals with several date methods. Usually when something goes wrong Xcode displays very dry messages such as &#8220;Array out of bounds&#8221; or something rather unhelpful (like the complete stack output with no clue as to what actually went wrong). I was accidentally passing nil into an [...]]]></description>
				<content:encoded><![CDATA[<p></p><p><img class="alignright size-thumbnail wp-image-2377" alt="Xcode_icon" src="http://wpguru.co.uk/wp-content/uploads/2012/12/Xcode_icon-150x150.png" width="150" height="150" />I&#8217;ve been working on an iOS App recently which deals with several date methods. Usually when something goes wrong Xcode displays very dry messages such as &#8220;Array out of bounds&#8221; or something rather unhelpful (like the complete stack output with no clue as to what actually went wrong).</p>
<p>I was accidentally passing nil into an NSDateComponents method &#8211; and instead of crashing (which is what I would have expected), Xcode displayed this super funny message in the log console:</p>
<blockquote><p><em>[__NSCFCalendar components:fromDate:toDate:options:]: </em></p>
<p><em>toDate cannot be nil</em></p>
<p><em>I mean really, what do you think that operation is supposed to mean with a nil toDate? </em></p>
<p><em>An exception has been avoided for now. A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.</em></p></blockquote>
<p>It made me smile <img src='http://wpguru.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><span id="more-3081"></span></p>
<p>I think we should have more error messages like this! Here&#8217;s a screenshot:</p>
<div id="attachment_3084" class="wp-caption aligncenter" style="width: 490px"><a href="http://wpguru.co.uk/wp-content/uploads/2013/03/Screen-Shot-2013-03-06-at-23.37.59.png"><img class="size-large wp-image-3084" alt="Xcode can be funny after all" src="http://wpguru.co.uk/wp-content/uploads/2013/03/Screen-Shot-2013-03-06-at-23.37.59-480x325.png" width="480" height="325" /></a><p class="wp-caption-text">Xcode can be funny after all</p></div>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/R07r2yfoD68" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/hilarious-xcode-error-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/hilarious-xcode-error-message/</feedburner:origLink></item>
		<item>
		<title>How to add some time to an NSDate</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/paL83wXT-v4/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-add-some-time-to-an-nsdate/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 04:07:03 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSCalendar]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateComponents]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-add-some-time-to-an-nsdate/</guid>
		<description><![CDATA[Imagine you had an NSDate object and want to add days several to it. We can use NSDate method dateByAddingComponents for this, which takes &#8211; as you may have guessed &#8211; NSDateComponents as parameters. In this example, let&#8217;s assume we want to know what date it is 5 days from today: int daysToAdd = 5; [...]]]></description>
				<content:encoded><![CDATA[<p>Imagine you had an NSDate object and want to add days several to it. We can use NSDate method dateByAddingComponents for this, which takes &#8211; as you may have guessed &#8211; NSDateComponents as parameters.</p>
<p>In this example, let&#8217;s assume we want to know what date it is 5 days from today: </p>
<pre class="wp-code-highlight prettyprint">
int daysToAdd = 5;
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc]init];
[components setDay:daysToAdd];
NSDate *futureDate = [gregorian dateByAddingComponents:components toDate:today options:0];
</pre>
<p>You can add as many and diverse components you like, such as</p>
<ul>
<li>setEra:</li>
<li>setYear:</li>
<li>setMonth:</li>
<li>setDay:</li>
<li>setHour:</li>
<li>setMinute:</li>
<li>setSecond:</li>
<li>setWeek:</li>
<li>setWeekday:</li>
<li>setWeekdayOrdinal:</li>
<li>setQuarter:</li>
<li>setCalendar:</li>
<li>setTimeZone:</li>
<li>setWeekOfMonth:</li>
<li>setWeekOfYear:</li>
<li>setYearForWeekOfYear:</li>
</ul>
<p>Check out the NSDateComponents Class Reference for more information.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/paL83wXT-v4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-add-some-time-to-an-nsdate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-add-some-time-to-an-nsdate/</feedburner:origLink></item>
		<item>
		<title>How to normalize NSDate objects (i.e. set the time to midnight)</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/VGQ0xz7CXC4/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-normalize-nsdate-objects-i-e-set-the-time-to-midnight/#comments</comments>
		<pubDate>Wed, 06 Mar 2013 16:18:40 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateComponents]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-normalize-nsdate-objects-i-e-set-the-time-to-midnight/</guid>
		<description><![CDATA[When you create new date using [NSDate date] (i.e. today, as in right now) your date will save the current time as well as its date. In fact, under the hood an NSDate object is the amount of seconds that have elapsed since the 1st of January 2001 (or 1970), in milliseconds. So really it&#8217;s [...]]]></description>
				<content:encoded><![CDATA[<p>When you create new date using [NSDate date] (i.e. today, as in right now) your date will save the current time as well as its date. </p>
<p>In fact, under the hood an NSDate object is the amount of seconds that have elapsed since the 1st of January 2001 (or 1970), in milliseconds. So really it&#8217;s a massive floating point number. You can see what it is with this code:</p>
<pre class="wp-code-highlight prettyprint">
NSLog(@&quot;Since 1970: %f&quot;, [today timeIntervalSince1970]);
NSLog(@&quot;Since 2001: %f&quot;, [today timeIntervalSinceReferenceDate]);
</pre>
<p>To save you the trouble, it&#8217;s bee roughly 1362585768.752258 seconds since 1970. But I digress&#8230;</p>
<p>Sometimes you want the date objects you&#8217;re dealing with not to regard &#8220;time&#8221; and really only deal with dates. As such, all dates &#8211; to be comparable &#8211; need to have the very same time, say midnight. Otherwise you may get the wrong results. Imagine adding three date objects that each have noon as their time, that&#8217;s an extra day and a half clouding your results right there.</p>
<p>Here&#8217;s some code that will normalize your NSDate object by setting its time to midnight:</p>
<pre class="wp-code-highlight prettyprint">
// normalizing a date
NSDate *yourDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:yourDate];
        
yourDate = [gregorian dateFromComponents:dateComponents];
        
NSLog(@&quot;Now your date starts at midnight: %@&quot;, yourDate);
</pre>
<p>In a nutshell, we &#8220;explode&#8221; the date into its year, month and day components, and then recreate a new object with these and no other components.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/VGQ0xz7CXC4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-normalize-nsdate-objects-i-e-set-the-time-to-midnight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-normalize-nsdate-objects-i-e-set-the-time-to-midnight/</feedburner:origLink></item>
		<item>
		<title>How to determine how many days / months / years have passed between two NSDate objects</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/XPlAWkVBgH8/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-determine-how-many-days-months-years-have-passed-between-two-nsdate-objects/#comments</comments>
		<pubDate>Wed, 06 Mar 2013 15:52:03 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSCalendar]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateComponents]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-determine-how-many-days-months-years-have-passed-between-two-nsdate-objects/</guid>
		<description><![CDATA[Imagine you had two NSDate objects and you&#8217;d like to find out the time interval between those dates. NSDate objects alone won&#8217;t help us out there unless we do some serious NSDateFormatting and hair pulling. Lucky for us there are a few other classes available that will help us do this, namely NSCalendar and NSDateComponents. [...]]]></description>
				<content:encoded><![CDATA[<p>Imagine you had two NSDate objects and you&#8217;d like to find out the time interval between those dates. NSDate objects alone won&#8217;t help us out there unless we do some serious NSDateFormatting and hair pulling.</p>
<p>Lucky for us there are a few other classes available that will help us do this, namely NSCalendar and NSDateComponents.</p>
<p>&nbsp;</p>
<h3>Years, Months and Days</h3>
<p>Here&#8217;s how you can determine how many years, months and days have passed between two NSDates:</p>
<pre class="wp-code-highlight prettyprint">NSDate *earlier = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:1];
NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

// pass as many or as little units as you like here, separated by pipes
NSUInteger units = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *components = [gregorian components:units fromDate:earlier toDate:today options:0];

NSInteger years = [components year];
NSInteger months = [components month];
NSInteger days = [components day];

NSLog(@&quot;Years: %ld, Months: %ld, Days: %ld&quot;, (long)years, (long)months, (long)days);</pre>
<p>The above example returns Years: 12, Months: 2, Days: 5.</p>
<p>In a nutshell we split the date into &#8220;components&#8221; such as years, months and days, and let the NSDateComponents class give us those as NSIntegers (i.e. long integers). For the class to calculate this correctly we need to put our days into the context of an NSCalendar (gregorian in our case). This is important because different calendars may return different time intervals.</p>
<p>&nbsp;</p>
<h3>Just the Days</h3>
<p>If you&#8217;re interested in just one particular item (days for example) just leave out any other component on this line:</p>
<pre class="wp-code-highlight prettyprint">// just the days
NSUInteger units = NSDayCalendarUnit;
</pre>
<h3></h3>
<h3>How about other units?</h3>
<p>You can pass as many of those fast enumeration units as you like, separated by pipes. For a full list of available values check out the NSCalendar Class Reference</p>
<p>&nbsp;</p>
<h3>Further Reading</h3>
<ul>
<li>NS Calendar Class Reference: <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html" rel="nofollow">https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html</a></li>
<li>Date and Time Programming Guide: <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html" rel="nofollow">https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/XPlAWkVBgH8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-determine-how-many-days-months-years-have-passed-between-two-nsdate-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-determine-how-many-days-months-years-have-passed-between-two-nsdate-objects/</feedburner:origLink></item>
		<item>
		<title>How to pop a UINavigationController in code</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/7fRzMDiW67c/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-pop-a-uinavigationcontroller-in-code/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 01:04:31 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-pop-a-uinavigationcontroller-in-code/</guid>
		<description><![CDATA[In UINavigationController speak, we &#8220;push&#8221; new controllers onto the stack (go forward), and we &#8220;pop&#8221; them off the stack (go back). The navigation controller handles all this for us. If you want to go back exactly one view controller, here&#8217;s how you can do it programmatically: // pop back to previous controller NSArray *myControllers = [...]]]></description>
				<content:encoded><![CDATA[<p>In UINavigationController speak, we &#8220;push&#8221; new controllers onto the stack (go forward), and we &#8220;pop&#8221; them off the stack (go back). The navigation controller handles all this for us.</p>
<p>If you want to go back exactly one view controller, here&#8217;s how you can do it programmatically:</p>
<pre class="wp-code-highlight prettyprint">
    // pop back to previous controller
    NSArray *myControllers = self.navigationController.viewControllers;
    int previous = myControllers.count - 2;
    UIViewController *previousController = [myControllers objectAtIndex:previous];
    
    [self.navigationController popToViewController:previousController animated:YES];
</pre>
<p>First we access the array of all view controllers and count it. We subtract one (because arrays are 0 based), and another one to go back in history &#8211; hence we go &#8220;minus 2&#8243;. Then we pop to view controller just behind the current one.</p>
<p>Test if the returned value is less than zero though&#8230;</p>
<p>If you want to go back all the way to the root view controller, there&#8217;s a method for that:</p>
<pre class="wp-code-highlight prettyprint">
    // pop back to root controller
    [self.navigationController popToRootViewControllerAnimated:YES];
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/7fRzMDiW67c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-pop-a-uinavigationcontroller-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-pop-a-uinavigationcontroller-in-code/</feedburner:origLink></item>
		<item>
		<title>How to “quick save” in Core Data</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/3AwtAVm8IIU/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-quick-save-in-core-data/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 00:26:53 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-save-in-core-data/</guid>
		<description><![CDATA[The easiest way to access a readily available save method is by importing AppDelegate into the class that wants to execute the save. Then we call a method in AppDelegate: AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; [myAppDelegate saveContext]; No reference to fetchedResultsController or managedObjectContext or any complex &#38;error statements.]]></description>
				<content:encoded><![CDATA[<p>The easiest way to access a readily available save method is by importing AppDelegate into the class that wants to execute the save. Then we call a method in AppDelegate:</p>
<pre class="wp-code-highlight prettyprint">
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[myAppDelegate saveContext];
</pre>
<p>No reference to fetchedResultsController or managedObjectContext or any complex &#038;error statements.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/3AwtAVm8IIU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-quick-save-in-core-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-quick-save-in-core-data/</feedburner:origLink></item>
		<item>
		<title>How to change the back button text on a UINavigationController</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/_CxH4VhHI6M/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 00:23:50 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[storyboard]]></category>
		<category><![CDATA[UINavigationBar]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/</guid>
		<description><![CDATA[In Your Storyboard I didn&#8217;t know the storyboard could do this, but it can: Select the Navigation Bar in question, then check the Attributes Inspector: In Code To set the title in code, you need to address the self.navigationItem.backBarButton property of your view controller and pass it a new UIBarButtonItem. Here&#8217;s one way of doing [...]]]></description>
				<content:encoded><![CDATA[<h3>In Your Storyboard</h3>
<p>I didn&#8217;t know the storyboard could do this, but it can: </p>
<p>Select the Navigation Bar in question, then check the Attributes Inspector:</p>
<p><img src="http://pinkstone.co.uk/wp-content/uploads/2013/02/Screen-Shot-2013-03-03-at-19.19.25.png" alt="Screen Shot 2013-03-03 at 19.19.25" width="314" height="167" class="aligncenter size-full wp-image-674" /></p>
<h3>In Code</h3>
<p>To set the title in code, you need to address the <strong>self.navigationItem.backBarButton</strong> property of your view controller and pass it a new UIBarButtonItem. Here&#8217;s one way of doing it:</p>
<pre class="wp-code-highlight prettyprint">
// set a different back button for the navigation controller
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc]init];
myBackButton.title = @&quot;Custom Text&quot;;
    
self.navigationItem.backBarButtonItem = myBackButton;
</pre>
<p>Alternatively we can use a much longer init method which will set the title in one line:</p>
<pre class="wp-code-highlight prettyprint">
// set a different back button for the navigation controller
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc]initWithTitle:@&quot;My Text&quot; style:UIBarButtonItemStyleBordered target:nil action:nil];
    
self.navigationItem.backBarButtonItem = myBackButton;
</pre>
<h3>One Thing of Note</h3>
<p>The back button (and its text) is only displayed in the NEXT view controller on the stack. So changing the back button in the detail view controller for example doesn&#8217;t change what&#8217;s displayed when the detail view controller is displayed. You must change it one step earlier, in the master controller&#8217;s navigation bar.</p>
<p><img src="http://pinkstone.co.uk/wp-content/uploads/2013/02/Screen-Shot-2013-03-03-at-19.22.06.png" alt="Screen Shot 2013-03-03 at 19.22.06" width="416" height="239" class="aligncenter size-full wp-image-675" /></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/_CxH4VhHI6M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/</feedburner:origLink></item>
		<item>
		<title>How to define a Protocol</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/leh3OQ-Hw_s/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-define-a-protocol/#comments</comments>
		<pubDate>Sun, 03 Mar 2013 22:58:18 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[protocol]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-define-a-protocol-2/</guid>
		<description><![CDATA[Protocols are a great way to delegate responsibility from one class to another. Consider a view controller that brings up another view controller via a modal segue. Let&#8217;s call them ViewController and DetailViewController. When it comes to dismissing that DetailViewController, he could do it himself via [self dismissViewControllerAnimated:YES completion:nil]; However, the first ViewController wouldn&#8217;t be [...]]]></description>
				<content:encoded><![CDATA[<p>Protocols are a great way to delegate responsibility from one class to another. </p>
<p>Consider a view controller that brings up another view controller via a modal segue. Let&#8217;s call them ViewController and DetailViewController. When it comes to dismissing that DetailViewController, he could do it himself via </p>
<pre class="wp-code-highlight prettyprint">
[self dismissViewControllerAnimated:YES completion:nil];
</pre>
<p>However, the first ViewController wouldn&#8217;t be in charge of that process. If we wanted to make that happen, we can define a protocol on DetailViewController, then have ViewController conform to it and hence be in charge of the dismissal.</p>
<p>Let&#8217;s make it happen!</p>
<h3>Defining the Protocol</h3>
<p>Most of the work happens in the header file of DetailViewController:</p>
<pre class="wp-code-highlight prettyprint">
#import &lt;UIKit/UIKit.h&gt;

@protocol DetailViewDelegate;

@interface DetailViewController : UIViewController

@property (nonatomic, weak) id &lt;DetailViewDelegate&gt; delegate;

- (IBAction)dismiss:(id)sender;

@end


@protocol DetailViewDelegate

- (void)detailViewDismiss;

@end
</pre>
<p>Right after the import statement, we say &#8220;we&#8217;ll define a protocol in here&#8221;. Next we setup a weak property called delegate so that we can declare a delegate elsewhere (other than self). </p>
<p>After the first @end statement we define one method that the delegate must confirm to. By default, all methods we define are &#8220;required&#8221; by the delegate &#8211; if we don&#8217;t implement them all, then we&#8217;ll get a yellow warning mark in Xcode.</p>
<p>We don&#8217;t have to define these methods in the implementation file. Instead, we would define where we would like our delegate to execute them. I have one method which will get called when a button on DetailViewController is pressed:</p>
<pre class="wp-code-highlight prettyprint">
- (IBAction)dismiss:(id)sender {
    
    [self.delegate detailViewDismiss];
}
</pre>
<p>All we do here is say &#8220;yo delegate, call my dismiss method and do in it whatever you like&#8221;. </p>
<h3>Confirming to the protocol</h3>
<p>We&#8217;ve done this countless times with built-in classes, and the process is exactly the same for our own protocols:</p>
<p>- add the delegate protocol in <Angle Brackets> to ViewController&#8217;s header file<br />
- implement the methods in the implementation file</p>
<p>Here&#8217;s the very simple ViewController.h header file (ie &#8220;the guy who&#8217;s in charge&#8221;):</p>
<pre class="wp-code-highlight prettyprint">
#import &lt;UIKit/UIKit.h&gt;
#import &quot;DetailViewController.h&quot;

@interface ViewController : UIViewController &lt;DetailViewDelegate&gt;

@end
</pre>
<p>And in the implementation file we&#8217;d define our only delegate method:</p>
<pre class="wp-code-highlight prettyprint">
- (void)detailViewDismiss {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
</pre>
<p>The only remaining thing is to tell DetailViewController who the delegate is. In case of a modal segue like ours this can be done in the prepareForSegue:sender method:</p>
<pre class="wp-code-highlight prettyprint">
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    DetailViewController *detailView = (DetailViewController *)[segue destinationViewController];
    detailView.delegate = self;
    
}
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/leh3OQ-Hw_s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-define-a-protocol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-define-a-protocol/</feedburner:origLink></item>
		<item>
		<title>How to dequeue UITableViewCells</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/4_jWta7d4QY/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-dequeue-uitableviewcells/#comments</comments>
		<pubDate>Sun, 03 Mar 2013 17:22:02 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewCell]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-dequeue-uitableviewcells-manually/</guid>
		<description><![CDATA[Sometimes Xcode does a bit of magic behind the scenes which I don&#8217;t really understand. Consider this standard table view method to display cells: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; // populate your cell return cell; } Where do we actually ever create a UITableViewCell? All we&#8217;re asking here [...]]]></description>
				<content:encoded><![CDATA[<p>Sometimes Xcode does a bit of magic behind the scenes which I don&#8217;t really understand. </p>
<p>Consider this standard table view method to display cells:</p>
<pre class="wp-code-highlight prettyprint">
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@&quot;Cell&quot;];

    // populate your cell
    
    return cell;
}
</pre>
<p>Where do we actually ever create a UITableViewCell? All we&#8217;re asking here is &#8220;do we have reusable ones available&#8221; &#8211; but if the answer is &#8220;no&#8221;, then somehow these get created without an error message. Other times (such as when using an overlay table view courtesy of the search display controller) we get an error message. </p>
<p>This brings me to the point that perhaps Xcode and iOS create as well as dequeue cells automatically &#8211; but when they don&#8217;t, here&#8217;s how we can do this manually. </p>
<p>Let&#8217;s amend the above code snippet:</p>
<pre class="wp-code-highlight prettyprint">
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@&quot;Cell&quot;];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@&quot;Cell&quot;];
    }

    // populate your cell
    
    return cell;
}
</pre>
<p>Now we properly create a cell should a dequeueable one not be available.</p>
<p>I haven&#8217;t had to use this, and the if statement never seems to get called if inserted into the Master/Detail template, yet I find this may be something to keep in mind for future projects.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/4_jWta7d4QY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-dequeue-uitableviewcells/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-dequeue-uitableviewcells/</feedburner:origLink></item>
		<item>
		<title>How to hide (and show) the UISearchBar in a UITableView</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/nQp2ywdv38k/</link>
		<comments>http://wpguru.co.uk/2013/03/how-to-hide-and-show-the-uisearchbar-in-a-uitableview/#comments</comments>
		<pubDate>Sat, 02 Mar 2013 18:23:38 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[UISearchBar]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-hide-and-show-a-uisearchbar-in-a-uitableview/</guid>
		<description><![CDATA[If you add a UISearchBar to your table view (see previous article) it&#8217;s just sitting there by default. Chances are you&#8217;d like to hide it when your table view first loads. You can do this by adding the following code to your viewWillAppear method: - (void)viewWillAppear:(BOOL)animated { // scroll the search bar off-screen CGRect newBounds [...]]]></description>
				<content:encoded><![CDATA[<p>If you add a UISearchBar to your table view (see previous article) it&#8217;s just sitting there by default. Chances are you&#8217;d like to hide it when your table view first loads.</p>
<p>You can do this by adding the following code to your viewWillAppear method:</p>
<pre class="wp-code-highlight prettyprint">
    - (void)viewWillAppear:(BOOL)animated {

    // scroll the search bar off-screen
    CGRect newBounds = self.tableView.bounds;
    newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
    self.tableView.bounds = newBounds;

}</pre>
<p>&nbsp;</p>
<p>Now it&#8217;s hidden, users make a search, hit cancel&#8230; and then it comes back into view. Wouldn&#8217;t it be nice if we could hide it again after users are finished with their search? And sure we can! We&#8217;re already conforming to the UISearchBarDelegate protocol, so all we need to do is to implement the following method and react to the cancel button:</p>
<p>&nbsp;</p>
<pre class="wp-code-highlight prettyprint">- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [self viewWillAppear:YES];

}</pre>
<p>We could implement the same code as above, or just call the viewWillAppear method again.</p>
<p>Note that you have to set the search bar&#8217;s delegate to your class for this to work. Searches are working fine when it&#8217;s not set, but to react to the cancel button it needs to be set (either in code or via the Connections Inspector in Interface Builder).</p>
<p>You can find a full demo project on <a href="https://github.com/versluis/Table-Seach-2013" >GitHub: Table Search 2013</a></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/nQp2ywdv38k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/03/how-to-hide-and-show-the-uisearchbar-in-a-uitableview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/03/how-to-hide-and-show-the-uisearchbar-in-a-uitableview/</feedburner:origLink></item>
		<item>
		<title>How to create a searchable UITableView</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/b1vFzSr_AW0/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-create-a-searchable-uitableview/#comments</comments>
		<pubDate>Fri, 01 Mar 2013 04:02:53 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[UISeachDisplayController]]></category>
		<category><![CDATA[UISearchBar]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/?p=645</guid>
		<description><![CDATA[The ingredients for a search function in a UITableView are more involved than just displaying a simple search field. First we need the standard UITableView. Next we need a UISearchBar which can be added to the top of the table view. But displaying the actual search results is something called the UISearchDisplayController. This process isn&#8217;t [...]]]></description>
				<content:encoded><![CDATA[<p>The ingredients for a search function in a UITableView are more involved than just displaying a simple search field. First we need the standard UITableView. Next we need a UISearchBar which can be added to the top of the table view. But displaying the actual search results is something called the UISearchDisplayController.</p>
<p>This process isn&#8217;t very well documented from what I could find, so here&#8217;s how I did it successfully. Works fine in iOS 6.</p>
<p>For iOS 5 compatibility please see my comment at the end.</p>
<p><span id="more-645"></span></p>
<p>This new controller &#8220;switches out&#8221; the standard table view and overlays a new one (the search display). Since the data for the search view is more or less the same as what was already powering the original table view (only &#8220;filtered&#8221;), the search view often uses the same data source as the original table view.</p>
<p>Speaking of filtering the original data: this is done with something called a predicate (NSPredicate). First time I&#8217;ve heard of this was in regards to Core Data. A predicate is a rather powerful yet mysterious thing and rest assured you don&#8217;t need to understand it in order to use it.</p>
<p>Let&#8217;s go through this step by step: from creating a standard table view with some dummy data, then adding a search function and slowly making it work.</p>
<p>&nbsp;</p>
<h3>Creating the Table View</h3>
<p>I&#8217;ll start with a Single View Application project (with Storyboards and ARC selected), removing the single View Controller from the Storyboard. Delete both ViewController h and m files too.</p>
<p>Next we&#8217;ll drag a UITableViewController into the Storyboard. Select the cell and set its reuse identifier to &#8220;Cell&#8221;. Next we&#8217;ll create a new Cocoa Touch Objective-C Class called TableViewController, naturally being a sub class of UITableViewController. Before we do anything else, let&#8217;s set this custom class in our Storyboard.</p>
<p>We&#8217;ll also create two properties in our h file:</p>
<ul>
<li>an NSArray called allData, holding all our data</li>
<li>an NSMutableArray called searchResults which will hold our filtered results</li>
</ul>
<p>Before we forget, let&#8217;s initialize the latter in our viewDidLoad method:</p>
<pre class="wp-code-highlight prettyprint">self.searchResults = [[NSMutableArray alloc]init];</pre>
<h3></h3>
<h3>Creating some Dummy Data</h3>
<p>Here&#8217;s a small method that initializes our data: 24 written out numbers. This could be anything really:</p>
<pre class="wp-code-highlight prettyprint">- (void)createData {

    self.allData = [[NSArray alloc]initWithObjects:@&quot;One&quot;, @&quot;Two&quot;, @&quot;Three&quot;, @&quot;Four&quot;, @&quot;Five&quot;, @&quot;Six&quot;,
                    @&quot;Seven&quot;, @&quot;Eight&quot;, @&quot;Nine&quot;, @&quot;Ten&quot;, @&quot;Eleven&quot;, @&quot;Twelve&quot;,
                    @&quot;Thirteen&quot;, @&quot;Fourteen&quot;, @&quot;Fifteen&quot;, @&quot;Sixteen&quot;, @&quot;Seventeen&quot;, @&quot;Eighteen&quot;,
                    @&quot;Ninteteen&quot;, @&quot;Twenty&quot;, @&quot;Twentyone&quot;, @&quot;Twentytwo&quot;, @&quot;Twentythree&quot;, @&quot;Twentyfour&quot;,
                    nil];
}</pre>
<p>Let&#8217;s call this method in our viewDidLoad so it&#8217;s ready for use as soon as the app starts:</p>
<pre class="wp-code-highlight prettyprint">[self createData];</pre>
<h3></h3>
<h3>Populating the Table View</h3>
<p>Let&#8217;s make our table view show those items: take out the two #warnings and all commented code to make our class a bit more readable. Next set the tableView:numberOfSections to 1 as we wont be dealing with sections here.</p>
<p>The tableView:numberOfRowsInSection needs to be the amount of our allData array, so we&#8217;ll make it look like this:</p>
<pre class="wp-code-highlight prettyprint">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
        return self.allData.count;
}</pre>
<p>And the final method we need to amend is the tableview:cellForRowAtIndexPath method so that text gets displayed in each cell:</p>
<pre class="wp-code-highlight prettyprint">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @&quot;Cell&quot;;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Configure the cell...
        cell.textLabel.text = [self.allData objectAtIndex:indexPath.row];

    return cell;
}</pre>
<p>Note that since Xcode 4.6 the template for a new Table View class has changed to incorporate an extremely annoying addition: right after the line with dequeueReusableCellWithIdentifier, we find &#8220;forIndexPath:indexPath&#8221;. This makes your app crash in iOS 5 and sometimes in iOS 6. Remove it to reflect my code above &#8211; you&#8217;ll avoid a lot of hair pulling later!</p>
<p>Run the app to see all our values listed as expected.</p>
<p>We&#8217;re not dealing with selecting them, but we&#8217;ll see next how we can make the view change into search mode.</p>
<h3></h3>
<h3>Bring on the big Search Bar</h3>
<p>This is where things get interesting: back in the storyboard, drag a UISearchDisplayController to the very top of your table view, so that it sits above the dummy cell. Switch to Assistant Editor mode and control drag a reference into your h file so we can address this thing.</p>
<p>Notice that when you do this you will only be able to create an outlet to a UISearchBar &#8211; that&#8217;s fine, it&#8217;s part of the UISearchDisplayController. Call it searchBar.</p>
<h3></h3>
<h3>Setting up our Predicate</h3>
<p>Back in the TableViewController.m file we need to add a couple of helper methods. The first one is a method called filterData. We will call this repeatedly so a small method will work best for us:</p>
<pre class="wp-code-highlight prettyprint">- (void)filterData {

    [self.searchResults removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@&quot;self CONTAINS [cd] %@&quot;, self.searchBar.text];
    self.searchResults = [[self.allData filteredArrayUsingPredicate:predicate] mutableCopy];

}</pre>
<p>This method will take what&#8217;s in our search bar and compare it to our allData array. Matching items are placed into our searchResults array (which we clear out at the beginning of this method). The [cd] statement means we don&#8217;t want to know about caSe sEnSitiVity or &#8220;diacritics&#8221; which are things like the Umlaut Dots above a letter and such.</p>
<p>The great thing for us is that after calling this method, we know how many matches we have and hence how many cells we need to display. We want to call this method every time someone adds a single letter to our search bar. And in order to know when that is, we better conform to the</p>
<h3></h3>
<h3>UISearchBar Delegate Protocol</h3>
<p>If we&#8217;re the Search Bar&#8217;s delegate, then we can implement a method that tells us when our search text changed. This is what we want! Head over to your TableViewController.h file and add the protocol declaration to the top:</p>
<pre class="wp-code-highlight prettyprint">@interface TableViewController : UITableViewController &amp;lt;UISearchBarDelegate&amp;gt;</pre>
<p>Back in the m file add this method, which in turn will call our filterData method:</p>
<pre class="wp-code-highlight prettyprint">- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    [self filterData];
}</pre>
<p>There&#8217;s no need to setup the delegate; since we&#8217;ve implemented a Search Display Controller, this has been done for us already.</p>
<h3></h3>
<h3>Dealing with the Search Display Controller</h3>
<p>This controller is rather clever: rather than bringing up a modal view or anything visually disruptive, the Search Display Controller &#8220;overlays&#8221; his own table view above out existing one, dimming the latter out. It&#8217;s slick alright &#8211; but therefore also a tad confusing. Because out of a sudden, we have TWO table views to deal with.</p>
<p>So when we tell the usual data source methods how many sections, rows and cells we need to display, we need to do so via an if-then statement so see which table view is requesting the data.</p>
<p>We&#8217;ll start with the tableView:numberOfRowsInSection method:</p>
<pre class="wp-code-highlight prettyprint">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.tableView) {
        return self.allData.count;
    }
    [self filterData];
    return self.searchResults.count;
}</pre>
<p>Here we ask: if it&#8217;s the &#8220;normal&#8221; table view, show the number of our allData array. If that&#8217;s not the case, we&#8217;ll be asked by the Search Display Controller. In which case, call the filterData method, then see how many items are, and then count those.</p>
<p>We need to do the same for our cellForRowAtIndexPath method:</p>
<pre class="wp-code-highlight prettyprint">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @&quot;Cell&quot;;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@&quot;Cell&quot;];
    }

    if (tableView == self.tableView) {
        // Configure the cell...
        cell.textLabel.text = [self.allData objectAtIndex:indexPath.row];
    } else {  
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    }    
    return cell;
}</pre>
<p>Again if the &#8220;standard&#8221; table view is asking, display an object from the allData. If it&#8217;s the other guy, display it from searchResults (by which time we would have just called the filterData method so we&#8217;re not calling it again).</p>
<p>One other point of note here is the if(!cell) statement:</p>
<p>Leaving this out makes the table view crash as soon as you hit a letter. For some reason cells don&#8217;t automatically create themselves as needed, which works fine in a standard table view. So if there aren&#8217;t any, we just create as many as needed.</p>
<h3></h3>
<h3>Full Project Code</h3>
<p>You can get the entire project on my repository on <a href="https://github.com/versluis/Table-Seach-2013" >GitHub: Table Search 2013</a></p>
<p>&nbsp;</p>
<h3>Further Reading</h3>
<ul>
<li><a href="http://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html" >TableSearch</a> &#8211; Apple&#8217;s Demo Code, last updated in 2010 and features iOS 4, does not make use of predicates (at the time of writing)</li>
<li><a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Predicates/predicates.html" >Predicates Programming Guide</a></li>
<li><a href="http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html" >UISearchDisplayController Class Reference</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/b1vFzSr_AW0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-create-a-searchable-uitableview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-create-a-searchable-uitableview/</feedburner:origLink></item>
		<item>
		<title>How to fix “this class is not key value coding-compliant” after accidentally adding an IBOutlet instead of an IBAction</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/UFne-Ewk5pU/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-fix-this-class-is-not-key-value-coding-compliant-after-accidentally-adding-an-iboutlet-instead-of-an-ibaction/#comments</comments>
		<pubDate>Sun, 24 Feb 2013 17:29:39 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[accidental outlet]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-fix-this-class-is-not-key-value-coding-compliant-after-accidentally-adding-an-iboutlet-instead-of-an-ibaction/</guid>
		<description><![CDATA[It has happened to us all: you&#8217;re in a storyboard, you&#8217;re using the Assistant Editor, you want to control-drag from a button into your code and create an action. But sadly you forget to select &#8220;Action&#8221; from the drop down menu and instead create an outlet. No biggie you think, deleting the outlet code. You [...]]]></description>
				<content:encoded><![CDATA[<p>It has happened to us all: you&#8217;re in a storyboard, you&#8217;re using the Assistant Editor, you want to control-drag from a button into your code and create an action.</p>
<p>But sadly you forget to select &#8220;Action&#8221; from the drop down menu and instead create an outlet.</p>
<p>No biggie you think, deleting the outlet code. You go back and create the action properly, run the app&#8230; and get an error message such as this one:</p>
<p><strong><br />
*** Terminating app due to uncaught exception &#8216;NSUnknownKeyException&#8217;, reason: &#8216;[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key badAction.&#8217;<br />
</strong></p>
<p>Yikes! Unless you&#8217;re using Version Control, there is no way to undo adding that bad outlet. So you ask yourself: Will I have to start from scratch, or is there a way to fix your app?</p>
<p>Yes this is fixable &#8211; all we need to do is examine our storyboard file in Source Code mode, then find the problem and eliminate it. Here&#8217;s how you do it:</p>
<ul>
<li>With the left pane open (Navigation Inspector, showing all your files), select your storyboard and right click/control click to bring up the context menu. Select View as Source Code</li>
</ul>
<p><img class="aligncenter size-large wp-image-641" alt="badAction1" src="http://pinkstone.co.uk/wp-content/uploads/2013/02/badAction1-600x283.png" width="600" height="283" /></p>
<p>This will allow you to see what the storyboard actually looks like in XML. Hit Option+F to bring up the Find in Context menu. Now search for whatever is causing you a headache, then delete the entire line. Make sure the opening and closing brackets are selected too.</p>
<p>In my case, all this needs to go:</p>
<pre class="wp-code-highlight prettyprint">&amp;lt;outlet property=&quot;badAction&quot; destination=&quot;4R8-F9-Xhb&quot; id=&quot;8xN-t2-22c&quot;/&amp;gt;</pre>
<p><img class="aligncenter size-large wp-image-642" alt="badAction2" src="http://pinkstone.co.uk/wp-content/uploads/2013/02/badAction2-600x439.png" width="600" height="439" /><br />
You may see more than one occurrence of your action in this file &#8211; we only want to get rid of the &#8220;outlet&#8221;reference here. If you delete all occurrences it&#8217;s not a big problem, but you will have to re-connect your actions to the code later.</p>
<p>Once you&#8217;re done, run the app again and all should be fine.</p>
<p>To display the storyboard in the visual editor again, just control-click the file file again, and select &#8220;Open as &#8211; Interface Builder, iOS Storyboard&#8221;.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/UFne-Ewk5pU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-fix-this-class-is-not-key-value-coding-compliant-after-accidentally-adding-an-iboutlet-instead-of-an-ibaction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-fix-this-class-is-not-key-value-coding-compliant-after-accidentally-adding-an-iboutlet-instead-of-an-ibaction/</feedburner:origLink></item>
		<item>
		<title>How to convert an NSString into an integer</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/CtZEAPETT50/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-convert-an-nsstring-into-an-integer/#comments</comments>
		<pubDate>Sat, 23 Feb 2013 14:13:08 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/?p=629</guid>
		<description><![CDATA[Imagine you had an NSString and wanted to save it as an integer value. You can use the intValue method for that: NSString *myString = @"47"; int myInt = [myString intValue]; To do the reverse, you can use the initWithFormat method: int myInt = 32; NSString *myOtherString = [[NSString alloc]initWithFormat:@"%i", myInt];]]></description>
				<content:encoded><![CDATA[<p>Imagine you had an NSString and wanted to save it as an integer value.<br />
You can use the intValue method for that:</p>
<pre class="wp-code-highlight prettyprint">
NSString *myString = @&quot;47&quot;;
int myInt = [myString intValue];
</pre>
<p>To do the reverse, you can use the initWithFormat method:</p>
<pre class="wp-code-highlight prettyprint">
int myInt = 32;
NSString *myOtherString = [[NSString alloc]initWithFormat:@&quot;%i&quot;, myInt];
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/CtZEAPETT50" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-convert-an-nsstring-into-an-integer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-convert-an-nsstring-into-an-integer/</feedburner:origLink></item>
		<item>
		<title>How to loop through every element in an NSArray</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/sVUCX3W_K8o/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-loop-through-every-element-in-an-nsarray/#comments</comments>
		<pubDate>Tue, 12 Feb 2013 05:01:05 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSArray]]></category>
		<category><![CDATA[NSDictionary]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-loop-through-every-element-in-an-nsarray/</guid>
		<description><![CDATA[We can use something called Fast Enumeration for this, using a modified for loop: // build an array NSArray *myArray = [[NSArray alloc]initWithObjects:@"One", @"Two", @"Three", @"Four", nil]; // loop through every element (dynamic typing) for (id tempObject in myArray) { NSLog(@"Single element: %@", tempObject); } You don&#8217;t have to use dynamic typing if you know [...]]]></description>
				<content:encoded><![CDATA[<p>We can use something called Fast Enumeration for this, using a modified for loop:</p>
<pre class="wp-code-highlight prettyprint">
// build an array
NSArray *myArray = [[NSArray alloc]initWithObjects:@&quot;One&quot;, @&quot;Two&quot;, @&quot;Three&quot;, @&quot;Four&quot;, nil];
// loop through every element (dynamic typing)
for (id tempObject in myArray) {
    NSLog(@&quot;Single element: %@&quot;, tempObject);
}
</pre>
<p>You don&#8217;t have to use dynamic typing if you know the types of objects you&#8217;re using. In our example this would work just as well:</p>
<pre class="wp-code-highlight prettyprint">
// build an array
NSArray *myArray = [[NSArray alloc]initWithObjects:@&quot;One&quot;, @&quot;Two&quot;, @&quot;Three&quot;, @&quot;Four&quot;, nil];
// loop through every element (static typing)
for (NSString *tempObject in myArray) {
    NSLog(@&quot;Single element: %@&quot;, tempObject);
}
</pre>
<p>Fast Enumeration also works with NSDictionaries of course:</p>
<pre class="wp-code-highlight prettyprint">
// build a dictionary
NSDictionary *myDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@&quot;One&quot;, @&quot;1&quot;, @&quot;Two&quot;,@&quot;2&quot;, @&quot;Three&quot;, @&quot;3&quot;,nil];
// loop through it (dynamic typing)
for (id tempObject in myDictionary) {
    NSLog(@&quot;Object: %@, Key: %@&quot;, [myDictionary objectForKey:tempObject], tempObject);
}
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/sVUCX3W_K8o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-loop-through-every-element-in-an-nsarray/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-loop-through-every-element-in-an-nsarray/</feedburner:origLink></item>
		<item>
		<title>How to generate a random number in iOS</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/uAM64NruHCE/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-generate-a-random-number-in-ios/#comments</comments>
		<pubDate>Sun, 10 Feb 2013 15:53:42 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[random number]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-generate-a-random-number-in-ios/</guid>
		<description><![CDATA[We can use the C function rand() for this: int i = rand()%10+1; NSLog(@"Random Number: %i", i); This generates an integer between 1 and 10. Alternatively you can use the arc4random function: int i = arc4random() % 10; NSLog(@"Random Number: %i", i); This will generate a random number between 0 and 9. One thing of [...]]]></description>
				<content:encoded><![CDATA[<p>We can use the C function rand() for this:</p>
<pre class="wp-code-highlight prettyprint">
int i = rand()%10+1;
NSLog(@&quot;Random Number: %i&quot;, i);
</pre>
<p>This generates an integer between 1 and 10. Alternatively you can use the arc4random function:</p>
<pre class="wp-code-highlight prettyprint">
int i = arc4random() % 10;
NSLog(@&quot;Random Number: %i&quot;, i);
</pre>
<p>This will generate a random number between 0 and 9.</p>
<p>One thing of note is that often this function will generate the same number multiple times in a row which may not be what you want.</p>
<p>If you search the Xcode documentation you&#8217;ll also find a link to Apple&#8217;s Randomization Services. Read it and weep here: <a href="http://developer.apple.com/library/ios/#documentation/Security/Reference/RandomizationReference/Reference/reference.html" rel="nofollow">http://developer.apple.com/library/ios/#documentation/Security/Reference/RandomizationReference/Reference/reference.html</a>.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/uAM64NruHCE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-generate-a-random-number-in-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-generate-a-random-number-in-ios/</feedburner:origLink></item>
		<item>
		<title>How to read the contents of a text file into an NSString</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/ny3oMzgLnRM/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-read-the-contents-of-a-text-file-into-an-nsstring/#comments</comments>
		<pubDate>Sun, 10 Feb 2013 05:41:06 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-read-the-contents-from-a-text-file-into-an-nsstring/</guid>
		<description><![CDATA[Imagine we had a file called myfile.txt which is a standard text file. On each line we have a new item we&#8217;d like to read so that our app can do something with it. Here&#8217;s how we do that: // get a reference to our file NSString *myPath = [[NSBundle mainBundle]pathForResource:@"myfile" ofType:@"txt"]; // read the [...]]]></description>
				<content:encoded><![CDATA[<p>Imagine we had a file called myfile.txt which is a standard text file. On each line we have a new item we&#8217;d like to read so that our app can do something with it. Here&#8217;s how we do that:</p>
<pre class="wp-code-highlight prettyprint">
// get a reference to our file
NSString *myPath = [[NSBundle mainBundle]pathForResource:@&quot;myfile&quot; ofType:@&quot;txt&quot;];

// read the contents into a string
NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];

// display our file
NSLog(&quot;Our file contains this: %@&quot;, myFile);
</pre>
<p>It is likely that instead of a long list of text we&#8217;d rather have each line in a separate string.<br />
Thankfully there&#8217;s a method for this:</p>
<pre class="wp-code-highlight prettyprint">
// split our string into an array
NSArray *mySplit = [myFile componentsSeparatedByString:@&quot;\n&quot;];

// display any one of them like this
NSLog(@&quot;Third Position: %@&quot;, [mySplit objectAtIndex:2]);
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/ny3oMzgLnRM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-read-the-contents-of-a-text-file-into-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-read-the-contents-of-a-text-file-into-an-nsstring/</feedburner:origLink></item>
		<item>
		<title>How to check for Network Connectivity in iOS</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/b_ZtRc_2Qr4/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-check-for-network-connectivity-in-ios/#comments</comments>
		<pubDate>Sat, 09 Feb 2013 01:12:04 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[network status]]></category>
		<category><![CDATA[Reachability]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-check-for-network-connectivity-in-ios/</guid>
		<description><![CDATA[What you really want is a method such as (BOOL)networkIsAvailable, returning YES or NO. Apple made it several megabytes long and requires you to study several years before you get to YES or NO. I hate that! This is such a basic function on an always-connected device that I find it appalling not to have [...]]]></description>
				<content:encoded><![CDATA[<p>What you really want is a method such as (BOOL)networkIsAvailable, returning YES or NO. Apple made it several megabytes long and requires you to study several years before you get to YES or NO. </p>
<p>I hate that! This is such a basic function on an always-connected device that I find it appalling not to have this available to all developers without hassle at all times.</p>
<p>Apple do give us some example code called Reachability in their documentation, which &#8211; like so many things &#8211; is hopelessly outdated and doesn&#8217;t work with ARC anymore: <a href="http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html" rel="nofollow">http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html</a></p>
<p>Thank god for the wonderful Tony Million who has updated the project and made it available on GitHub: <a href="https://github.com/tonymillion/Reachability" rel="nofollow">https://github.com/tonymillion/Reachability</a> &#8211; works with ARC and everything. It&#8217;s a simple drop-in class, exactly what we all want and need.</p>
<p>I salute you, Tony! Thank you very much <img src='http://pinkstone.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Here&#8217;s how to add it to your project step by step:</p>
<ol>
<li>add both Reachability.h and Reachability.m to your project</li>
<li>add the SystemConfiguration.framework to your project</li>
<li>in the class that needs to check for network connectivity, #import Reachability.h</li>
<li>create a method such as this to check which network connection you&#8217;ve got:</li>
</ol>
<pre class="wp-code-highlight prettyprint">
- (void)checkForNetwork
{
    // check if we&#039;ve got network connectivity
    Reachability *myNetwork = [Reachability reachabilityWithHostname:@&quot;google.com&quot;];
    NetworkStatus myStatus = [myNetwork currentReachabilityStatus];
    
    switch (myStatus) {
        case NotReachable:
            NSLog(@&quot;There&#039;s no internet connection at all. Display error message now.&quot;);
            break;
            
            case ReachableViaWWAN:
            NSLog(@&quot;We have a 3G connection&quot;);
            break;
            
            case ReachableViaWiFi:
            NSLog(@&quot;We have WiFi.&quot;);
            break;
    
        default:
            break;
    }
}
</pre>
<p>Obviously check which host you&#8217;d like to reach. There are several other methods in this class, such as reachabilityForInternetConnection. Check out Tony&#8217;s examples on GitHub for more:</p>
<p><a href="https://github.com/tonymillion/Reachability" rel="nofollow">https://github.com/tonymillion/Reachability</a></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/b_ZtRc_2Qr4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-check-for-network-connectivity-in-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-check-for-network-connectivity-in-ios/</feedburner:origLink></item>
		<item>
		<title>How to delete a file</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/EtKBiTofT8g/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-delete-a-file/#comments</comments>
		<pubDate>Wed, 06 Feb 2013 22:22:06 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSFileManager]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-delete-a-file/</guid>
		<description><![CDATA[Once you have a reference to the file in question you can call the removeItemAtPath method: // get reference with path and filename NSString *filePath = [[NSString alloc]initWithFormat:@"Documents/myFile.txt"]; NSString *deleteThis = [NSHomeDirectory()stringByAppendingPathComponent:filePath]; // now delete the file [[NSFileManager defaultManager]removeItemAtPath:deleteThis error:nil]; This does not work with wildcards in the file name (for example *.jpg).]]></description>
				<content:encoded><![CDATA[<p>Once you have a reference to the file in question you can call the removeItemAtPath method:</p>
<pre class="wp-code-highlight prettyprint">
// get reference with path and filename
NSString *filePath = [[NSString alloc]initWithFormat:@&quot;Documents/myFile.txt&quot;];
NSString *deleteThis = [NSHomeDirectory()stringByAppendingPathComponent:filePath];
// now delete the file
[[NSFileManager defaultManager]removeItemAtPath:deleteThis error:nil];
</pre>
<p>This does not work with wildcards in the file name (for example *.jpg).</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/EtKBiTofT8g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-delete-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-delete-a-file/</feedburner:origLink></item>
		<item>
		<title>How to test if a file exists</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/14O-v_IaYZs/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-test-if-a-file-exists/#comments</comments>
		<pubDate>Mon, 04 Feb 2013 14:35:49 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSFileManager]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-test-if-a-file-exists/</guid>
		<description><![CDATA[Imagine we had a file that we&#8217;ve saved: NSString *myFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myFile.txt"]; Before accessing it we can determine if it exists or not: BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory()stringByAppendingPathComponent:myFile]];]]></description>
				<content:encoded><![CDATA[<p>Imagine we had a file that we&#8217;ve saved:</p>
<pre class="wp-code-highlight prettyprint">
NSString *myFile = [NSHomeDirectory() stringByAppendingPathComponent:@&quot;Documents/myFile.txt&quot;];
</pre>
<p>Before accessing it we can determine if it exists or not:</p>
<pre class="wp-code-highlight prettyprint">
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory()stringByAppendingPathComponent:myFile]];
</pre>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/14O-v_IaYZs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-test-if-a-file-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-test-if-a-file-exists/</feedburner:origLink></item>
		<item>
		<title>How to access the iPhone Simulator Directory Structure</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/MaurZisXF-w/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-access-the-iphone-simulator-directory-structure/#comments</comments>
		<pubDate>Mon, 04 Feb 2013 14:16:24 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[iOS simulator]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-access-the-iphone-simulator-directory-structure/</guid>
		<description><![CDATA[You can see the full directory under ~Library/Application Support/iPhone Simulator You&#8217;ll be presented with a list of iOS Versions, each of which has several folders to explore. To access data that your apps have written, head over to Applications and see a list of cryptic folders, each one corresponding to one of your apps. In [...]]]></description>
				<content:encoded><![CDATA[<p>You can see the full directory under </p>
<p><strong>~Library/Application Support/iPhone Simulator</strong></p>
<p>You&#8217;ll be presented with a list of iOS Versions, each of which has several folders to explore. To access data that your apps have written, head over to Applications and see a list of cryptic folders, each one corresponding to one of your apps. In each you&#8217;ll find your BundleID.app, indicating which folder belongs to which app.</p>
<p>The Documents folder for example would contain all files that your app has saved to its local Document folder.</p>
<p>Library only comes up in finder by holding down the Option key, then clicking Go from the top menu.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/MaurZisXF-w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-access-the-iphone-simulator-directory-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-access-the-iphone-simulator-directory-structure/</feedburner:origLink></item>
		<item>
		<title>How to update an iOS device from a Beta to a Release Version</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/uuMjGj3MkmQ/</link>
		<comments>http://wpguru.co.uk/2013/02/how-to-update-an-ios-device-from-a-beta-to-a-release-version/#comments</comments>
		<pubDate>Sun, 03 Feb 2013 21:02:47 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[expired beta]]></category>
		<category><![CDATA[ipsw]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-update-an-ios-device-from-a-beta-to-a-release-version/</guid>
		<description><![CDATA[When a new iOS version comes out, developers can get pre-release Beta Versions to test their apps. New betas can be installed over the air, but if the release version comes out then there&#8217;s no over the air upgrade for those. Instead, it appears that you already have the latest version installed &#8211; even though [...]]]></description>
				<content:encoded><![CDATA[<p>When a new iOS version comes out, developers can get pre-release Beta Versions to test their apps. New betas can be installed over the air, but if the release version comes out then there&#8217;s no over the air upgrade for those. Instead, it appears that you already have the latest version installed &#8211; even though it&#8217;s not the release version.</p>
<p>At the same time a Release goes public, previous Betas expire &#8211; requiring them to be replaced on the iOS device in question.</p>
<p>Just like Beta Versions can downloaded at <a href="http://developer.apple.com/" rel="nofollow">http://developer.apple.com</a>, Release Versions can be downloaded there too (.ipsw files) once they become available. Here&#8217;s how to install them via iTunes:</p>
<ol>
<li>download the .ipsw for your device model (there are a growing number, so double-check)</li>
<li>open iTunes and connect the device via USB</li>
<li>Option-Click the &#8220;Check for Update&#8221; button</li>
<li>this brings up a dialogue that lets you select the .ipsw file</li>
</ol>
<p><img class="aligncenter size-large wp-image-604" alt="option-click" src="http://pinkstone.co.uk/wp-content/uploads/2013/02/option-click-600x148.png" width="600" height="148" /></p>
<p><img class="aligncenter size-full wp-image-601" alt="Screen Shot 2013-02-03 at 15.57.35" src="http://pinkstone.co.uk/wp-content/uploads/2013/01/Screen-Shot-2013-02-03-at-15.57.35.png" width="523" height="385" /></p>
<p><img class="aligncenter size-full wp-image-602" alt="Screen Shot 2013-02-03 at 15.57.58" src="http://pinkstone.co.uk/wp-content/uploads/2013/01/Screen-Shot-2013-02-03-at-15.57.58.png" width="604" height="278" /></p>
<p>The same procedure goes if a new Beta is released. This will put your device into recovery mode, and even though it&#8217;s a rather distressing process, if all goes well no data is lost in the process.</p>
<h3>What if this doesn&#8217;t work?</h3>
<p>Sometimes I&#8217;ve encountered that my device STILL can&#8217;t activate after doing the above. If that&#8217;s the case, repeat the above, Option-Clicking on Restore. This will also let you choose the .ipsw file, however it will erase all your data.</p>
<p>You can choose to click Restore without Option, which will download the latest firmware for you.</p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/uuMjGj3MkmQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/02/how-to-update-an-ios-device-from-a-beta-to-a-release-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/02/how-to-update-an-ios-device-from-a-beta-to-a-release-version/</feedburner:origLink></item>
		<item>
		<title>How to create an NSTimer</title>
		<link>http://feedproxy.google.com/~r/IosDevDiary/~3/v5AXFVNMoU4/</link>
		<comments>http://wpguru.co.uk/2013/01/how-to-create-an-nstimer/#comments</comments>
		<pubDate>Thu, 31 Jan 2013 23:28:06 +0000</pubDate>
		<dc:creator>Jay Versluis</dc:creator>
				<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[NSTimer]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pinkstone.co.uk/how-to-create-an-nstimer/</guid>
		<description><![CDATA[Timers are good if you want to delay executing a method, or if you need to call a method repeatedly. To set a one-off timer without setting a property you can use this: // calls the updateLabel method in 5 seconds [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:NO]; However, you won&#8217;t be able to stop it [...]]]></description>
				<content:encoded><![CDATA[<p>Timers are good if you want to delay executing a method, or if you need to call a method repeatedly. To set a one-off timer without setting a property you can use this:</p>
<pre class="wp-code-highlight prettyprint">
// calls the updateLabel method in 5 seconds
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:NO];
</pre>
<p>However, you won&#8217;t be able to stop it without a reference. This is fine for one-off timers, but you need one if you want to stop a repeating timer.</p>
<p>To start a repeating timer self.myTimer:</p>
<pre class="wp-code-highlight prettyprint">
// calls the updateLabel method every 0.1 seconds
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
</pre>
<p>And to stop if:</p>
<pre class="wp-code-highlight prettyprint">
// stop the timer
[self.myTimer invalidate];
</pre>
<p>Here&#8217;s a working example on GitHub: <a href="https://github.com/versluis/TimerFun" rel="nofollow">https://github.com/versluis/TimerFun</a></p>
<img src="http://feeds.feedburner.com/~r/IosDevDiary/~4/v5AXFVNMoU4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.uk/2013/01/how-to-create-an-nstimer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://wpguru.co.uk/2013/01/how-to-create-an-nstimer/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.828 seconds. --><!-- Cached page generated by WP-Super-Cache on 2013-05-16 22:56:37 --><!-- Compression = gzip -->
