<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">
	<title>Damien DeVille</title>
	
	<link href="http://www.ddeville.me" />
	<updated>2012-01-10T18:10:25+00:00</updated>
	<id>http://www.ddeville.me/</id>
	<author>
		<name>Damien DeVille</name>
	</author>
	<image>
		<url>/static/images/apple-touch-icon.png</url>
		<title>Damien DeVille</title>
		<link>http://www.ddeville.me</link>
	</image>
	
	
	<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/DamienDeVille" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="damiendeville" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
		<title>Stephen Wolfram on the .data domain for the Data Web</title>
		
		<link href="http://blog.stephenwolfram.com/2012/01/a-data-top-level-internet-domain/" />
		
		<updated>2012-01-10T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2012/01/towards-the-data-web</id>
		<content type="html">&lt;p&gt;Fascinating concept.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>NSURLConnection setDelegateQueue is broken on iOS</title>
		
		<link href="http://www.ddeville.me/2011/12/broken-NSURLConnection-on-ios" />
		
		<updated>2011-12-29T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/broken-NSURLConnection-on-ios</id>
		<content type="html">&lt;p&gt;On Mac OS 10.7 and iOS 5.0, a very handy method has been added to NSURLConnection: &lt;em&gt;setDelegateQueue:&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Using a URL connection in an NSOperation subclass has always been quite tedious because of the fact that one has to run a runloop for as long as the delegate callbacks have to happen.&lt;!--more--&gt;
As suggested by &lt;a href="http://twitter.com/mzarra"&gt;Marcus Zarra&lt;/a&gt; in this StackOverflow &lt;a href="http://stackoverflow.com/a/7492587"&gt;answer&lt;/a&gt;, this has usually been achieved by calling CFRunLoopRun() when calling the start: method on NSURLConnection and CFRunLoopStop(CFRunLoopGetCurrent()) when the connection finishes loading or fails.&lt;/p&gt;

&lt;p&gt;However, as my colleague &lt;a href="http://twitter.com/keith_duncan"&gt;Keith Duncan&lt;/a&gt; pointed out on &lt;a href="http://www.cocoabuilder.com/archive/cocoa/281178-running-nsurlconnection-from-within-an-nsoperation.html"&gt;CocoaBuilder&lt;/a&gt;, this is not optimal and should be avoided.&lt;/p&gt;

&lt;p&gt;So, by creating an NSOperationQueue in your NSOperation subclass and scheduling the delegate callbacks into this queue, we do not need to run the current runloop for the length of the operation and consume a thread. In a nutshell, this is very handy and a much cleaner way to achieve this!&lt;/p&gt;

&lt;p&gt;Here comes the problem: even though everything works perfectly fine on Lion, when performing the very same thing on iOS, the delegate callbacks are never called. It looks like the connection has to be started (and the delegate queue set) on the main queue for the delegate callbacks to happen. This obviously defeats the point of using it within an NSOperation subclass given that since Snow Leopard (and iOS 4.0) the &lt;em&gt;start&lt;/em&gt; method of NSOperation will likely not be called on the main thread.&lt;/p&gt;

&lt;p&gt;For this reason, in my opinion, the &lt;em&gt;setDelegateQueue:&lt;/em&gt; method is unusable as per iOS 5.0 and should be avoided until Apple fixes this problem.&lt;/p&gt;

&lt;p&gt;I have filed a bug at &lt;a href="http://openradar.appspot.com/10529053"&gt;rdar://10529053&lt;/a&gt; and you should dupe it if you observe the same behavior as I do.&lt;/p&gt;

&lt;p&gt;I have created a small sample project to demonstrate the bug that I have attached to the radar. Feel free to run it on your side and see for yourself. You can download it from &lt;a href="http://media.ddeville.me/static/files/posts/2011/12/BrokenConnection.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>MIME type to UTI and back again in Cocoa</title>
		
		<link href="http://www.ddeville.me/2011/12/mime-to-UTI-cocoa" />
		
		<updated>2011-12-28T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/mime-to-UTI-cocoa</id>
		<content type="html">&lt;p&gt;Often when working with web services in Cocoa, I need to convert between MIME type and Uniform Type Identifier (UTI).&lt;/p&gt;

&lt;p&gt;I have seen many hacky way to do this but, in my opinion, following is the correct way to achieve it.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;First let's import the appropriate framework based on the platform (you might also need to add the framework to your project if not previously done).&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#if TARGET_OS_IPHONE&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;lt;MobileCoreServices/MobileCoreServices.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#else&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;lt;CoreServices/CoreServices.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#endif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;To get the UTI from a MIME type.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;NSURLResponse&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="c1"&gt;// assume a URL response from somewhere else.&lt;/span&gt;
&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;responseMIMEType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="n"&gt;MIMEType&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;CFStringRef&lt;/span&gt; &lt;span class="n"&gt;MIMEType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__bridge&lt;/span&gt; &lt;span class="n"&gt;CFStringRef&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="n"&gt;MIMEType&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;CFStringRef&lt;/span&gt; &lt;span class="n"&gt;UTI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UTTypeCreatePreferredIdentifierForTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kUTTagClassMIMEType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MIMEType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;UTIString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__bridge_transfer&lt;/span&gt; &lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;UTI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;And to get the MIME type from the UTI.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="c1"&gt;// assume the path to a file from somewhere else.&lt;/span&gt;
&lt;span class="n"&gt;CFStringRef&lt;/span&gt; &lt;span class="n"&gt;fileExtension&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__bridge&lt;/span&gt; &lt;span class="n"&gt;CFStringRef&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt; &lt;span class="n"&gt;pathExtension&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;CFStringRef&lt;/span&gt; &lt;span class="n"&gt;UTI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UTTypeCreatePreferredIdentifierForTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kUTTagClassFilenameExtension&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fileExtension&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;CFStringRef&lt;/span&gt; &lt;span class="n"&gt;MIMEType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UTTypeCopyPreferredTagWithClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UTI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kUTTagClassMIMEType&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;CFRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UTI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;MIMETypeString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__bridge_transfer&lt;/span&gt; &lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;MIMEType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
	</entry>
	
	<entry>
		<title>Sophiestication Apps on Sales</title>
		
		<link href="http://itunes.apple.com/artist/sophiestication-software/id284935449?mt=8" />
		
		<updated>2011-12-22T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/sophiestication-sales</id>
		<content type="html">&lt;p&gt;All Sophiestication Software apps are $0.99 for the Christmas period.&lt;/p&gt;

&lt;p&gt;I own all of them and am a big fan of Groceries and Articles for both iPhone and iPad in particular. Go check them out!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Updated Realmac Company Page</title>
		
		<link href="http://www.realmacsoftware.com/company" />
		
		<updated>2011-12-20T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/new-realmac-company-page</id>
		<content type="html">&lt;p&gt;Check out the updated company page at &lt;a href="http://www.realmacsoftware.com/company" title="Realmac Software Company Page!"&gt;Realmac Software&lt;/a&gt; and our new avatars!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://media.ddeville.me/static/images/posts/2011/12/realmac_damien_analog.jpg"&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/12/realmac_damien_analog_thumb.jpg" title="My page on the new Realmac company page, processed with Analog ;)" alt="My page on the new Realmac company page, processed with Analog ;)" /&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>UCL Computer Science Short Courses</title>
		
		<link href="http://www-typo3.cs.ucl.ac.uk/short_courses/" />
		
		<updated>2011-12-06T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/ucl-cs-short-courses</id>
		<content type="html">&lt;p&gt;The UCL Computer Science department has released a list of short courses for the summer of 2012.&lt;/p&gt;

&lt;p&gt;The Time-Series Modelling one looks quite nice (and would remind me of the time when I used to be a student there actually!).
I also like Popular Writing. Many of us can indeed very well write when targeting a technical audience but writing for the general public is definitely another story.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>New Google+ for iOS icon</title>
		
		<link href="http://www.ddeville.me/2011/12/new_google_plus_for_ios_icon" />
		
		<updated>2011-12-06T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/new_google_plus_for_ios_icon</id>
		<content type="html">&lt;p&gt;&lt;a href="http://media.ddeville.me/static/images/posts/2011/12/google_plus.png"&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/12/google_plus_thumb.png" title="New Goole+ for iOS icon" alt="New Google+ for iOS icon" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Google+ for iOS. Keeping up with good design and inconsistencies.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Alfred app goes 1.0 on the Mac App Store</title>
		
		<link href="http://itunes.apple.com/gb/app/alfred/id405843582?mt=12" />
		
		<updated>2011-12-06T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/alfred-app-goes-1</id>
		<content type="html">&lt;p&gt;Today, Alfred app version 1.0 has been released on the Mac App Store (it's actually been available to download from their website for a couple of days).&lt;/p&gt;

&lt;p&gt;I love Alfred. I've never been a fan of Quicksilver or LaunchBar, always preferring the simpler Spotlight but Alfred seriously got me!&lt;/p&gt;

&lt;p&gt;Also, if you feel like you want to give some money to the developers, make sure you check &lt;a href="http://www.alfredapp.com/powerpack"&gt;Alfred Powerpack&lt;/a&gt;. Plenty of awesome additions.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>LLVM 3.0 Release Notes</title>
		
		<link href="http://llvm.org/releases/3.0/docs/ReleaseNotes.html" />
		
		<updated>2011-12-02T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/llvm-3-release-notes</id>
		<content type="html">&lt;p&gt;Check out the new goodies.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Birds vs Chickens</title>
		
		<link href="http://www.ddeville.me/2011/12/birds-vs-chickens" />
		
		<updated>2011-12-01T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/12/birds-vs-chickens</id>
		<content type="html">&lt;p&gt;&lt;a href="http://media.ddeville.me/static/images/posts/2011/12/birds_vs_chickens.png"&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/12/birds_vs_chickens_thumb.png" title="Birds vs Chickens" alt="Birds vs Chickens on the App Store" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is getting seriously ridiculous.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Welcome to the New Way to Navigate Google</title>
		
		<link href="http://www.ddeville.me/2011/11/new-way-to-navigate-google" />
		
		<updated>2011-11-30T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/11/new-way-to-navigate-google</id>
		<content type="html">&lt;p&gt;&lt;a href="http://media.ddeville.me/static/images/posts/2011/12/google_home_page.png"&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/12/google_home_page_thumb.png" title="Google Home" alt="Google Home" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not sure I like it.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Analog Update on the Mac App Store</title>
		
		<link href="http://itunes.apple.com/gb/app/analog/id418343177?mt=12" />
		
		<updated>2011-11-01T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/11/analog-update-on-the-mas</id>
		<content type="html">&lt;p&gt;The 1.01 version of Analog has just been approved this morning and is available on the Mac App Store. Update through the Mac App Store or if you didn't purchase it yet, &lt;a href="http://itunes.apple.com/gb/app/analog/id418343177?mt=12"&gt;go get it&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://media.ddeville.me/static/images/posts/2011/11/analog_update_app_store.png"&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/11/analog_update_app_store_thumb.png" title="Analog Update" alt="Analog Update" /&gt;&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>A Sister's Eulogy for Steve Jobs</title>
		
		<link href="http://www.nytimes.com/2011/10/30/opinion/mona-simpsons-eulogy-for-steve-jobs.html" />
		
		<updated>2011-10-30T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/a-sisters-eulogy-for-steve-jobs</id>
		<content type="html">&lt;p&gt;A very poignant article from Mona Simpson, Steve Jobs' sister. Such a touching story, extremely well narrated.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Recursive Blocks in Objective-C</title>
		
		<link href="http://www.ddeville.me/2011/10/recursive-blocks-objc" />
		
		<updated>2011-10-29T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/recursive-blocks-objc</id>
		<content type="html">&lt;p&gt;Blocks are an awesome feature added to Objective-C with Mac OS X 10.6 and iOS 4.0.&lt;/p&gt;

&lt;p&gt;Most of the Cocoa API now includes block-based alternatives and some new APIs (like the AssetsLibrary framework for example) are only based on blocks. So you do not have a choice anymore, you &lt;em&gt;have to&lt;/em&gt; use blocks.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;One cool feature that you might run into is recursive blocks. Even if there is in fact no reason why a block could not be recursive, there are a couple of rules to follow. An example of a recursive block would be something like the following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;blocky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Note that this piece of code has a couple of problems, apart from the fact that it loops forever, but explaining them being the point of this post we will discuss them later (no spoilers!).&lt;/p&gt;

&lt;p&gt;So why would we ever want to use a block recursively? Well, think about a common pattern in Cocoa, particularly in CocoaTouch. You have a UINavigationController on which you push instances of subclasses of UITableViewController in order to display some sort of tree structure of data. This structure will most likely be fetched from Core Data and resides in a single place in your application, probably in the root view controller of your navigation controller.&lt;/p&gt;

&lt;p&gt;Now suppose that each of these instances of (subclasses of) UITableViewController has to perform a specific action when one of its cells is clicked. It could be modifying the cell, pushing a new UITableViewController on the navigation stack or even do nothing. Since, as we have seen, the whole data structures would be owned by a single object (the root view controller), you do not really want controller down the stack to have to decide what to do with this data. You would rather have the root view controller, that actually manages the data, react to events down the chain.&lt;/p&gt;

&lt;p&gt;You could have a simple protocol in your UITableViewController subclass that the root view controller would conform to but you would end up with your logic for cell selection handling residing in two spots: in the table view cell selection callback in your root view controller and in the table view controller subclass delegate methods in this very same root view controller.&lt;/p&gt;

&lt;p&gt;So why not having a block that handles the cell selection in the root view controller and passing this block down the stack. Once a cell down the navigation controller is clicked, the controller subclass simply invokes that block and the root view controller will perform anything it has to do at that stage.&lt;/p&gt;

&lt;p&gt;So let's write some (pseudo)code. First, assume that we will use the same subclass of UITableViewController for any level of navigation down the stack. At the end, it probably will display the same kind of data so why create a custom subclass for each level of navigation. We will also have a simple class representing a data object to be displayed. Think about it as the data backing up the content of your cells. These objects are probably organized in an NSArray that itself resides in the root view controller. Each data object has a children array containing other data objects further down in the data hierarchy.&lt;/p&gt;

&lt;p&gt;First the very elementary MyDataObject class:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;MyDataObject&lt;/span&gt; : &lt;span class="nc"&gt;NSObject&lt;/span&gt;

&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strong&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;MyDataObject&lt;/span&gt;

&lt;span class="k"&gt;@synthesize&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Next, the UITableViewController subclass used to manage the table view used to display content down the navigation stack:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;CellSelectionHandlerBlock&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;MyDataObject&lt;/span&gt; &lt;span class="n"&gt;clickedItem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;MyTableViewControllerSubclass&lt;/span&gt; : &lt;span class="nc"&gt;UITableViewController&lt;/span&gt;

&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strong&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MyDataObject&lt;/span&gt; &lt;span class="n"&gt;dataObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;CellSelectionHandlerBlock&lt;/span&gt; &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;MyTableViewControllerSubclass&lt;/span&gt;

&lt;span class="k"&gt;@synthesize&lt;/span&gt; &lt;span class="n"&gt;dataObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_dataObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@synthesize&lt;/span&gt; &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_selectionHandlerBlock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;tableView:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UITableView&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;tableView&lt;/span&gt; &lt;span class="nl"&gt;didSelectRowAtIndexPath:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSIndexPath&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;indexPath&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_selectionHandlerBlock&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;MyDataObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;clickedDataObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[[&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt; &lt;span class="n"&gt;dataObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;objectAtIndex:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;indexPath&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
        &lt;span class="n"&gt;_selectionHandlerBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clickedDataObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// some other methods, in particular UITableView dataSource and delegate&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;And finally the root view controller, itself a subclass of UITableViewController, that manages the table view used at the first level of hierarchy. It also manages the data objects and handle the cell selection in both its own table view and down the stack:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;MyRootTableViewController&lt;/span&gt; : &lt;span class="nc"&gt;UITableViewController&lt;/span&gt;

&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strong&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dataObjects&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;MyRootTableViewController&lt;/span&gt;

&lt;span class="k"&gt;@synthesize&lt;/span&gt; &lt;span class="n"&gt;dataObjects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_dataObjects&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;tableView:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UITableView&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;tableView&lt;/span&gt; &lt;span class="nl"&gt;didSelectRowAtIndexPath:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSIndexPath&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;indexPath&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CellSelectionHandlerBlock&lt;/span&gt; &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyDataObject&lt;/span&gt; &lt;span class="n"&gt;clickedItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="n"&gt;clickedItem&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;MyTableViewControllerSubclass&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;viewController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;MyTableViewControllerSubclass&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;viewController&lt;/span&gt; &lt;span class="nl"&gt;setDataObject:&lt;/span&gt; &lt;span class="n"&gt;clickedItem&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;viewController&lt;/span&gt; &lt;span class="nl"&gt;setSelectionHandlerBlock:&lt;/span&gt; &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt; &lt;span class="n"&gt;navigationController&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;pushViewController:&lt;/span&gt; &lt;span class="n"&gt;viewController&lt;/span&gt; &lt;span class="nl"&gt;animated:&lt;/span&gt; &lt;span class="n"&gt;YES&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;Do some funky stuff with the cell!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;// like presenting a modal view controller with some content.&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="n"&gt;MyDataObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dataObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt; &lt;span class="n"&gt;dataObjects&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;objectAtIndex:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;indexPath&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
    &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// some other methods, in particular UITableView dataSource and delegate&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;So as you can see, we only need to write the selection handling code once. In function of the data object underlying the clicked cell, the root view controller create a new MyTableViewControllerSubclass with the appropriate data object and pass the selection handler block to it. Whenever a cell is clicked in a view controller down the stack, we first retrieve the data object corresponding to the clicked cell and invoke the block.&lt;/p&gt;

&lt;p&gt;Also, every time a table view cell is clicked in the root view controller itself, the very same block is invoked.&lt;/p&gt;

&lt;p&gt;However, this piece of code creating the block is wrong for two reasons that I am going to explain. As you can see, the block is recursively called inside the block itself. In order to explain the problems with the current approach, let's get back to our simpler &lt;em&gt;blocky&lt;/em&gt; example.&lt;br/&gt;
In this example, a block is created and then called inside the content of the block creating infinite recursion. Leaving aside the infinite part here, this piece of code would crash on the first recursive call. To understand why, let's first discuss how a block treats its enclosing scope variables. Consider the following example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;i = %i&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This piece of code would correctly print &lt;em&gt;i = 1&lt;/em&gt; but would crash on the second line &lt;em&gt;i++&lt;/em&gt;. The reason is that the block captures local variables in its scope and treat them as constant.&lt;br/&gt;
In the case of Objective-C objects, captured objects are retained (and automatically released when the block goes away).&lt;br/&gt;
Blocks are actually Objective-C objects themselves. When first created, a block lives on the stack. If you intend to use the block beyond the current scope, you need to copy it to the heap. Once the block has been copied to the heap, it can be retained and released as a regular object (it does not make sense to retain a stack-based block though).&lt;br/&gt;
In order for a block to change its enclosing scope, we can use the __block storage type. Consider the following example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;__block&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;i = %i&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;i = %i&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In this case, the program will run correctly and print &lt;em&gt;i = 1&lt;/em&gt; and &lt;em&gt;i = 2&lt;/em&gt;. The block was able to modify the variable in its enclosing scope. Also note that __block captured objects are not retained.&lt;/p&gt;

&lt;p&gt;So coming back to our &lt;em&gt;blocky&lt;/em&gt; example, we now understands better why the crash is occurring. Since the block itself is invoked inside the block, it is const copied even before the assignment occurs. We can fix this by rewriting the program as:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;__block&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;blocky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Similarly, in our navigation controller example, we would have to rewrite:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;__block&lt;/span&gt; &lt;span class="n"&gt;CellSelectionHandlerBlock&lt;/span&gt; &lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;selectionHandlerBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyDataObject&lt;/span&gt; &lt;span class="n"&gt;clickedItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;So far so good, and to be honest, during my testing, I did not have to do anything else for this to run perfectly in Debug mode. However, the first time I ran it in Release mode, my program crashed on the block invocation. It took me a while to understand what was going on until I realized that there was a small problem here.&lt;br/&gt;
I was reusing my block outside the current scope and even copied it later on (when passed to another objects).&lt;br/&gt;
If you think about it, the first block that is assigned to the __block type variable is a stack-based block. The block that is called inside the block is this very same stack-based block.&lt;br/&gt;
When the block is moved to the heap, captured variable are moved to the heap too. So our block variable is moved to the heap once the block is copied so we need to make sure that the reference variable points to the copy of the block and not the original stack-based one.&lt;/p&gt;

&lt;p&gt;The solution to this problem is to copy the block while creating it and assign this copied version to our __block variable:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;__block&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;blocky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;blocky&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I have no idea why this was not crashing in Debug.&lt;/p&gt;

&lt;p&gt;Blocks are very handy but are a tricky concept to grasp at first. However, there are a very powerful tool and I can only tell you that once you go block you never go bock (sorry, that was lame).&lt;/p&gt;

&lt;p&gt;I definitely recommend the following articles and books to learn more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://twitter.com/nevyn"&gt;Joachim Bengtsson&lt;/a&gt; - &lt;a href="http://thirdcog.eu/pwcblocks/"&gt;Programming with C Blocks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://twitter.com/bbum"&gt;Bill Bumgarner&lt;/a&gt; - &lt;a href="http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/"&gt;^ Blocks Tips &amp;amp; Tricks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://twitter.com/borkware"&gt;Mark Dalrymple&lt;/a&gt; - &lt;a href="http://www.amazon.co.uk/Advanced-Mac-OSX-Programming-Guides/dp/0321706250/ref=sr_1_1?ie=UTF8&amp;amp;qid=1319917894&amp;amp;sr=8-1"&gt;Advanced Mac OS X Programming&lt;/a&gt; chapter 3 "Blocks"&lt;/li&gt;
&lt;/ul&gt;

</content>
	</entry>
	
	<entry>
		<title>Moving to Jekyll</title>
		
		<link href="http://www.ddeville.me/2011/10/moving-to-jekyll" />
		
		<updated>2011-10-28T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/moving-to-jekyll</id>
		<content type="html">&lt;p&gt;I haven't had much time to blog about it since I made the switch but you might have noticed the new feel and look of my site!&lt;/p&gt;

&lt;p&gt;I have moved away from WordPress for various reasons but mainly because I really did not like the idea of having my content living in a database. Also, I wanted to code the CSS myself and did not particularly want to go through the whole WP theme stuff.&lt;/p&gt;

&lt;p&gt;So, the story in a nutshell, I am now generating a static version of this site with &lt;a href="https://github.com/mojombo/jekyll"&gt;Jekyll&lt;/a&gt;. I write all the posts and bits in Markdown that I store as plain text files. I have written all the CSS using &lt;a href="http://sass-lang.com/"&gt;Sass&lt;/a&gt; (highly recommended by the way). I store my media files on Amazon S3 that I access with Amazon Cloud Front. I use &lt;a href="https://typekit.com"&gt;TypeKit&lt;/a&gt; for serving and managing the fonts and the &lt;a href="http://www.mathjax.org/"&gt;MathJax&lt;/a&gt; JavaScript display engine for rendering the mathematics formulae written with LaTeX. I am also using &lt;a href="http://disqus.com"&gt;DISQUS&lt;/a&gt; for the comments on the posts.&lt;/p&gt;

&lt;p&gt;I use quite a few new goodies of HTML 5 and CSS 3 so the site will render best on WebKit and fairly alright in Firefox. I checked that the site looked ok on the latest version of IE but I have no idea how it does on former versions. But frankly, I don't even give a rat's ass!&lt;/p&gt;

&lt;p&gt;I am very happy with the look of the site on a desktop browser and Safari on the iPad (and I guess this applies to all tablet even though I haven't tested it). However, I still will have to write a mobile stylesheet for the iPhone (and other smartphones).&lt;/p&gt;

&lt;p&gt;The very cool stuff is that I can now version control my entire website.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Norman's Dream Device</title>
		
		<link href="http://www.ddeville.me/2011/10/normans-dream-device" />
		
		<updated>2011-10-27T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/normans-dream-device</id>
		<content type="html">&lt;p&gt;Donald A. Norman in &lt;em&gt;The Design of Everyday Things&lt;/em&gt;, 1988:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Would you like a pocket-size device that reminded you of each appointment and daily event? I would. I am waiting for the day when portable computers become small enough that I can keep one with me at all times. I will definitely put all my reminding burdens upon it. It has to be small. It has to be convenient to use. And it has to be relatively powerful, at least at today's standards. It has to have a full, standard typewriter keyboard and a reasonably large display. It needs good graphics, because that makes a tremendous difference in usability, and a lot of memory - a huge amount actually. And it should be easy to hook up to the telephone; I need to connect it to my home and laboratory computers. Of course, it should be relatively inexpensive.&lt;/p&gt;

&lt;p&gt;What I ask for is not unreasonable. The technology I need is available today. It's just that the full package has never been put together, partly because the cost in today's world would be prohibitive. But it will exist in imperfect form in five years, possibly in perfect form in ten.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;In my opinion, even if PDAs have existed for quite a long time, almost 30 years later, the first device that actually meets every criteria mentioned by Norman (apart from the cost maybe) is the iPhone.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Min Nya Fjällräven Kånken</title>
		
		<link href="http://www.ddeville.me/2011/10/min-nya-fjallraven-kanken" />
		
		<updated>2011-10-27T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/min-nya-fjallraven-kanken</id>
		<content type="html">&lt;p&gt;Yesterday I received my new laptop bag. I am really satisfied with it so I thought I would share my story.&lt;/p&gt;

&lt;p&gt;Deciding on a bag is tricky. It's quite hard to find an item that matches your criteria for appearance, comfort and usability.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Since high school, I have been using a &lt;a href="https://www.manhattanportage.com/catalog/Shoulder-Bags/DJ-Bags/70"&gt;DJ bag&lt;/a&gt; form Manhattan Portage in black. It fits perfectly a 15'' laptop. I used it through most of university and have been quite happy with it.
When I got my first 13'' MacBook, I logically got another bag from Manhattan Portage, a &lt;a href="http://www.manhattanportage.com/catalog/Backpacks/School-Backpack/191"&gt;School Backpack&lt;/a&gt; that fits a 13'' laptop (and has a special compartment for it). I have been using this bag for a few years and absolutely loved it.&lt;/p&gt;

&lt;p&gt;Last year, my girlfriend gave me a Swedish bag apparently quite trendy over there, a Fjällräven Kånken, the &lt;a href="http://www.ilovemykanken.com/shop/products/kanken-classic-ox-red.htm"&gt;classic&lt;/a&gt; model in red. This bag is very fun, comfy and of very (very!) good quality (this was a second hand for I don't know which year but sincerely, I wouldn't know how to break the damn thing!). It fits a 13'' laptop quite alright, even if there is no special pocket for the laptop. I have used it for nearly a year and enjoyed it a lot. I also used it as my every day bag when my girlfriend and I went on a trip one month in Brazil last May and it's been a great companion!&lt;/p&gt;

&lt;p&gt;However, when I got a 15'' MacBook Pro, I was not able to use it anymore so I had to get back to my DJ bag and outch! I forgot how uncomfortable a shoulder bag is versus a backpack. So I had to hunt for a new bag.&lt;/p&gt;

&lt;p&gt;Tough times.. I couldn't decide on a model that I found attractive and that would take great care of my laptop (and my back). Until I realized that Fjällräven Kånken were not only doing the classic model but had recently released a 15'' laptop backpack. So I got &lt;a href="http://www.ilovemykanken.com/shop/products/fjallraven-kanken-laptop-15-black.htm"&gt;one&lt;/a&gt; in black.
It basically matches the appearance of the classic but is a bit bigger and also have a special back pocket for the laptop. You can thus use the front part of the bag as you would do with a normal bag but you have extra storage specially for your laptop against your back (the pocket actually matches perfectly the 15'' MackBook Pro so I guess they designed the bag with this laptop in mind). The back pocket is sensational; enough saying that it's the first time I dare putting my laptop into a bag without a sleeve (the laptop + the sleeve wouldn't fit in the back pocket anyway).&lt;/p&gt;

&lt;p&gt;I highly recommend this bag. If you are interested, they also do the laptop backpack model for 13'' and 17'', just check their &lt;a href="http://www.ilovemykanken.com/shop/shop-by-style/"&gt;store&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>My Thoughts About iMessage</title>
		
		<link href="http://www.ddeville.me/2011/10/thoughts-about-imessage" />
		
		<updated>2011-10-23T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/thoughts-about-imessage</id>
		<content type="html">&lt;p&gt;When iOS 5 came out, I logically turned on iMessage on both my iPhone and iPad. What I liked with iMessage was not being able to send free texts (I have 500 texts included in my plan for free and rarely use more than 50-100 a month so cost is definitely not in the equation here), but rather the idea of having my messages delivered (and stored) to all my devices seamlessly.&lt;/p&gt;

&lt;p&gt;Well, at first, my iMessages were not delivered to both devices but only on the iPhone. We later discovered we had to set the callerID to the email rather than the phone number, that should work. Well, that solves the problem is someone iMessages me to my email address but if an iMessage is sent as a regular text to my phone number, I still only receive it on my iPhone.&lt;/p&gt;

&lt;p&gt;The major problem so far, however, has been reliability. I don't seem to receive every message sent to me. Also, I often sent a message to a friend that seems to be routed via iMessage. The message seems to be sent, nothing comes back (the user might not have turned on the "Send Read Receipt" setting). However, 30 minutes later I receive an alert from my carrier (I am on pay-as-you-go hence the alerts) that a text has been sent. I go to my messages and discover that the message has been sent as Text Message. I'm ok with that, the only problem is that &lt;strong&gt;it took 30 minutes for my text to reach its destination without myself knowing about it&lt;/strong&gt;. This is definitely a problem for me. I use text messages for fast communication with friends and I really expect any text I send to be received the second it leaves my device.&lt;/p&gt;

&lt;p&gt;I have disabled iMessage for now. I will keep looking at it and I can wait to enable it back, but it seems not reliable enough at the moment.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Whale Trail</title>
		
		<link href="http://www.ddeville.me/2011/10/whale-trail" />
		
		<updated>2011-10-22T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/10/whale-trail</id>
		<content type="html">&lt;p&gt;With all the repetitive, low quality games hitting the App Store these days, I am very rarely surprised by a new one. Once on a while, however, a simple game comes out and blows my mind away. Whale Trail is one of these.&lt;/p&gt;

&lt;p&gt;The concept of Whale Trail is nothing really innovative. You are some sort of a flying whale (!) and you fly up and down (in a similar fashion as Tiny Wing), trying to catch bubbles. Bubbles actually give you power and when you run out of power, well, you loose. There are also evil clouds that when you fly into electrocutes you and consequently diminish your power.&lt;/p&gt;

&lt;p&gt;More than the concept, what really makes Whale Trail such an amazing game are the graphics and the music. Both are extremely well curated. &lt;a href="http://www.ustwo.co.uk/"&gt;UsTwo&lt;/a&gt; really took extreme care in creating both an ambient music and sound effects that completely enhance the game experience.&lt;/p&gt;

&lt;p&gt;Go get it on the &lt;a href="http://itunes.apple.com/gb/app/whale-trail/id450163154?mt=8"&gt;App Store&lt;/a&gt;!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>My First Bit</title>
		
		<link href="http://www.ddeville.me/2011/08/bit-1" />
		
		<updated>2011-08-21T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/08/bit-1</id>
		<content type="html">&lt;p&gt;This is my first bit. I will blog in bits rather than posts whenever I have some short stories that I want to talk about that wouldn't fit in (a series of) tweets but wouldn't be long enough to become a proper blog post.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>My Backup Solution</title>
		
		<link href="http://www.ddeville.me/2011/08/my-backup-solution" />
		
		<updated>2011-08-11T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2011/08/my-backup-solution</id>
		<content type="html">&lt;p&gt;I recently decided to have a serious look at my backup strategy.&lt;br/&gt;
For the past few years, I have simply been using a Time Machine hard disk that I have been trying to plug to my laptop at least daily. I have also been keeping my code in sync to GitHub as much as possible.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Given that my Time Machine hard disk is sitting on my desk, this backup solution is not very robust since in case of fire or theft (knock on wood!), I would not be protected at all. My code would surely be easily to get back from GitHub but I write way more code than I can host in my private repositories on GitHub. Thus, I decided to take a step forward and here is my current backup strategy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Machine&lt;/strong&gt;: I own a Western Digital 2TB hard drive which is partitioned into two 1TB parts, one of them being exclusively for Time Machine (I store mainly media on the second half). It supports Firewire 800 which makes Time Machine backups super fast with my Macbook Pro.
I try to keep it plugged to the MBP as much as possible during the day but I make sure that it’s plugged at least once everyday, most of the time at night.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dropbox&lt;/strong&gt;: My main Documents folder is pretty much empty since I use Dropbox for my documents. All my work related stuff, old university coursework and notes, photos are also stored on Dropbox. Having multiple computers, I use Dropbox extensively and am very happy with it. I pay $9.99/month for 50GB and I seriously think it is a bargain given the quality of the service.&lt;br/&gt;
I have also set up a Library folder in my root folder where I store Application Support data for various applications that I want to access from multiple computers such as 1 Password, Things, Papers and MoneyWell.&lt;br/&gt;
Finally, I have a Code Collection folder where I store code samples I found around the Internet or small projects I have been messing around and that do not require a proper version control.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Amazon S3&lt;/strong&gt;: I currently use &lt;em&gt;Arq.app&lt;/em&gt; for uploading hourly backups of my drive to Amazon S3 storage. Arq is an excellent software and I’m extremely happy with it. It uploads in the background through an agent (Arg Agent) and is pretty fast. The backups are also encrypted which is a very neat feature.&lt;br/&gt;
I have opted for an Amazon S3 monthly budget of $5 which gives me 53.76GB of storage and seems to meet my space requirements. Also, Amazon recently made the data transfer-in free so there is no fee for uploading the backups to S3. Data transfer-out still costs a few cents per GB but this should rarely happen giving that the data is a backup. As you understood, the total monthly cost is mainly the storage.&lt;br/&gt;
It is recommended to backup the whole User directory but I preferred to pick the folders to backup manually. Note that I have an extensive Music folder (over 100GB) that for obvious reasons I do not backup to S3. It is however part of my Time Machine backup and I hope to be able to transfer it to the cloud soon once iTunes offers it. If it did not happen this fall, I might decide to back it up to S3 though.&lt;br/&gt;
The folders that I backup are thus as following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;/Library/Application Support&lt;/li&gt;
&lt;li&gt;/Applications&lt;/li&gt;
&lt;li&gt;~/Desktop&lt;/li&gt;
&lt;li&gt;~/Documents&lt;/li&gt;
&lt;li&gt;~/Dropbox&lt;/li&gt;
&lt;li&gt;~/Library (excluding Caches and Logs)&lt;/li&gt;
&lt;li&gt;~/Movies&lt;/li&gt;
&lt;li&gt;~/Pictures/iPhoto Library&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GitHub&lt;/strong&gt;: I have a GitHub micro plan ($7/month) which allows me to keep up to 5 private repositories. My main projects are stored on GitHub. I have added the public keys associated to my various machines to GitHub which allows me to push and pull from any of my computers. I am extremely happy with GitHub since the first day I have started using it!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Gitosis on Linode&lt;/strong&gt;: I have a Linode VPS that I use to store my websites, web services and messing around. I have setup a Git “server” with Gitosis and there I store any project that requires Version Control but not important enough to be a proper private repository on GitHub. Setting up Gitosis was a bit of a pain on the ass but now that it is up and working, I am quite satisfied. Similarly, I have uploaded the public keys of my various machines to be able to push and pull.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I am now fairly confident with my backups. I am easily able to revert changes locally with Time Machine or even from the cloud with Arq (both these backups are done hourly).&lt;br/&gt;
In case of hard disk failure or theft on my Macbook Pro, I can easily restore the whole disk from Time Machine.&lt;br/&gt;
In case my Time Machine hard disk is also corrupted, I can now restore my main files and configuration from Amazon S3 with Arq.&lt;br/&gt;
Every piece of code I write is also version controlled so reverting mistakes is trivial.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>DDProgressView, a Custom Progress View</title>
		
		<link href="http://www.ddeville.me/2011/03/ddprogressview-a-custom-progress-view" />
		
		<updated>2011-03-21T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/03/ddprogressview-a-custom-progress-view</id>
		<content type="html">&lt;p&gt;I was quite bored of the classic blue UIProgressView provided in UIKit so I decided to write a custom progress view, very much inspired by the one used in the official Twitter for iPhone app.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;It lets you specify the tint color of both the inner and outer parts of the control. You can use DDProgressView exactly as you would use a classic UIProgressView.
In the future I might add support for different shapes too.&lt;/p&gt;

&lt;p&gt;You can find the source code on &lt;a href="http://github.com/ddeville/DDProgressView" title="Github"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/05/uiprogressview.png" title="DDProgressView" alt="DDProgressView" /&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Add Variables to an Existing Class in Obj-C</title>
		
		<link href="http://www.ddeville.me/2011/03/add-variables-to-an-existing-class-in-objective-c" />
		
		<updated>2011-03-14T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/03/add-variables-to-an-existing-class-in-objective-c</id>
		<content type="html">&lt;p&gt;Categories are a great way to add functionality to existing classes in Objective-C without extending them.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Say you want to add a method to MD5 hash a particular string. A Java way of doing this would be to create your own string class by subclassing NSString and add the hashMD5 method to it. With Objective-C, you don't have to subclass at all, you can simply write a category on NSString that declares the hashMD5 method. Then, you'll be able to call the hashMD5 method on any NSString instance, assuming you correctly imported the category header.
Categories are very elegant and powerful, however there is a drawback with them: you cannot add instance variables! The interface/implementation of a category looks like following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="nl"&gt;(StringAdditions)&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;


&lt;span class="cp"&gt;#import &amp;quot;NSString+StringAdditions.h&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="nl"&gt;(StringAdditions)&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;As you can see, there is no room for instance variables! So you think you'll maybe be able to add a property to it. You can indeed declare the property in the interface with&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;retain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;coolString&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;but if you try to synthesize it in the implementation, you will get an error "@synthesize not allowed in a category implementation". The problem is that @synthesize will create a getter and setter for us but by doing that, it will also have to create a instance variable of the same name where to store the value and get it from, hence the problem.&lt;/p&gt;

&lt;p&gt;However, starting with Snow Leopard and iOS 3.1, the objective-C runtime adds Associated Object support. It might sound a bit of an obscure term but it literally adds some magic!
From the Apple runtime documentation, it allows you to "Returns the value associated with a given object for a given key" and "Sets an associated value for a given object using a given key and association policy". Let see it with an example: assume you want to set a default string that your hashMD5 method would return in case it cannot compute the hash (not very pertinent example, sorry about that ;)).
You would then define the retained defaultHash string property as following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="nl"&gt;(StringAdditions)&lt;/span&gt;

&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;retain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;defaultHash&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;printString&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;As we've seen earlier, you won't be able to @synthesize this property in your implementation so let's write the getter and setter ourselves. You need to make sure that you correctly import the objective-c runtime header.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#import &amp;quot;NSString+StringAdditions.h&amp;quot;&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;lt;objc/runtime.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="nl"&gt;(StringAdditions)&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;defaultHashKey&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;defaultHash&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;objc_getAssociatedObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;defaultHashKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;setDefaultHash:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;aString&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;objc_setAssociatedObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;defaultHashKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;aString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OBJC_ASSOCIATION_RETAIN_NONATOMIC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;printString&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;the string is: %@&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defaultHash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;So, let's see what we've done here. We first declare a static char which address will act as the (constant) key to our variable. We then defined the getter &lt;em&gt;defaultHash&lt;/em&gt; and setter &lt;em&gt;setDefaultHash:&lt;/em&gt;. In the getter, we simply return the object associated with our object (in this case &lt;em&gt;self&lt;/em&gt; which is an instance of NSString) and the address of the key. Similarly, in the setter we set the object associated with our object, the given key (again the constant address of the char we declared earlier) and the memory policy that will match what you declared in the property (in our case nonatomic and retain). If you have a look at the runtime header, you can see that the following is defined, allowing you to match every kind of memory policy you are used to use in you properties.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;OBJC_ASSOCIATION_ASSIGN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;OBJC_ASSOCIATION_RETAIN_NONATOMIC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;OBJC_ASSOCIATION_COPY_NONATOMIC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;OBJC_ASSOCIATION_RETAIN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;01401&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;OBJC_ASSOCIATION_COPY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;01403&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;uintptr_t&lt;/span&gt; &lt;span class="n"&gt;objc_AssociationPolicy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;If you now try to do the following in another class in you project&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#import &amp;quot;NSString+StringAdditions.h&amp;quot;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nl"&gt;setDefaultHash:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;Ciao&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;printString&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;you should see the following printed in the console: &lt;em&gt;the string is: Ciao&lt;/em&gt;. Magical!&lt;/p&gt;

&lt;p&gt;As a bonus, here are the function declaration from the runtime header&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;OBJC_EXPORT&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;objc_setAssociatedObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;objc_AssociationPolicy&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;__OSX_AVAILABLE_STARTING&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__MAC_10_6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__IPHONE_3_1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;OBJC_EXPORT&lt;/span&gt; &lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="n"&gt;objc_getAssociatedObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;__OSX_AVAILABLE_STARTING&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__MAC_10_6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__IPHONE_3_1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;OBJC_EXPORT&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;objc_removeAssociatedObjects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;__OSX_AVAILABLE_STARTING&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__MAC_10_6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__IPHONE_3_1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Hope that helps ;)&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Create an Encrypted ZIP File on Mac OS X</title>
		
		<link href="http://www.ddeville.me/2011/02/create-an-encrypted-zip-file-on-mac-os-x" />
		
		<updated>2011-02-11T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/02/create-an-encrypted-zip-file-on-mac-os-x</id>
		<content type="html">&lt;p&gt;I recently needed to password encrypt a ZIP file on my Mac. Most of the free GUI software (Stuffit, Unarchiver, etc..) can only unarchive and decrypt file but you have to purchase a premium version in order to encrypt a file.&lt;!--more--&gt;
Fortunately, if you're ok with using the Terminal, Mac OS X already comes with an easy way to password encrypt a ZIP file. Simply type in a Terminal&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;zip -ejr &lt;span class="o"&gt;[&lt;/span&gt;name_of_archive&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;path_to_file&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;where &lt;em&gt;name_of_archive&lt;/em&gt; is the name that you want to give to your ZIP file and &lt;em&gt;path_to_file&lt;/em&gt; is the complete path to the file or folder that you want to compress.
You will then be asked for a password that will indeed be the password used to decrypt the archive.
In the &lt;em&gt;ejr&lt;/em&gt; options, &lt;em&gt;e&lt;/em&gt; stands for encrypt, &lt;em&gt;j&lt;/em&gt; for junk-paths (do not store directory names) and &lt;em&gt;r&lt;/em&gt; for recursive-paths (travel the directory structure recursively).&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Creating Shared Instances of Non Thread-Safe Classes</title>
		
		<link href="http://www.ddeville.me/2011/02/creating-shared-instances-of-non-thread-safe-classes" />
		
		<updated>2011-02-03T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/02/creating-shared-instances-of-non-thread-safe-classes</id>
		<content type="html">&lt;p&gt;Often, when coding, you find yourself allocating the very same object again and again.
A common use in order to avoid wasting resources is to store a static reference to a single instance of this class and keep returning it when needed.&lt;!--more--&gt;
A classic example is NSDateFormatter. Assume you have a UITableView displaying a date in each UITableViewCell in a particular format. You might have the date stored in a database but you will need an instance of NSDateFormatter to be able to transform it into a string the way you intend. Obviously, creating an instance of NSDateFormatter with the same NSLocale and format for each cell of your UITableView is wasting resources and memory.
This is the reason why many programmers use the following sort of method&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="nl"&gt;setLocale:&lt;/span&gt; &lt;span class="p"&gt;[[[&lt;/span&gt;&lt;span class="n"&gt;NSLocale&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithLocaleIdentifier:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;en_GB&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;autorelease&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="nl"&gt;setDateFormat:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;YYYY-MM-dd HH:mm:ss ZZZ&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This way, whenever you need an instance of NSDateFormatter, call&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;dateFormatter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;to get a reference to the shared NSDateFormatter.&lt;/p&gt;

&lt;p&gt;The problem with this method is that NSDateFormatter is not thread-safe. This means that if you happen to call this method from different threads, you might get unexpected behaviors.
Obviously, you are not left with creating single instances for every time you need one, there is an easy way to create an instance for every thread you call this method from. This way, you won't have to create as many objects as the number of times you call the method but simply as many objects as the number of different threads your are calling the method from.
To achieve this, we use a cool feature in NSThread:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSMutableDictionary&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;threadDictionary&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The thread dictionary is a classic dictionary where you can store thread-specific data. Any thread has got its own dictionary and anyone is free to store any interesting data.
We can thus rewrite our shared NSDateFormatter as following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;currentDateFormatter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSMutableDictionary&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;threadDictionary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;NSThread&lt;/span&gt; &lt;span class="n"&gt;currentThread&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;threadDictionary&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;threadDictionary&lt;/span&gt; &lt;span class="nl"&gt;objectForKey:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;DDMyDateFormatter&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;NSDateFormatter&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="nl"&gt;setLocale:&lt;/span&gt; &lt;span class="p"&gt;[[[&lt;/span&gt;&lt;span class="n"&gt;NSLocale&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithLocaleIdentifier:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;en_GB&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;autorelease&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="nl"&gt;setDateFormat:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;YYYY-MM-dd HH:mm:ss ZZZ&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;threadDictionary&lt;/span&gt; &lt;span class="nl"&gt;setObject:&lt;/span&gt; &lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="nl"&gt;forKey:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;DDMyDateFormatter&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dateFormatter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Let's go through this method. We first get a reference to the current thread dictionary. We then check whether we previously store an instance of NSDateFormatter with a specific key. In case we did, we simply return it. If it is the first time we access this method from this thread, the dictionary won't have any NSDateFormatter object stored for the key and we will simply create one and store it in the dictionary before returning it.
Easy!&lt;/p&gt;

&lt;p&gt;PS: For the same reason, I also have to implement this method for NSCalendar.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>A Cocoa App for Resizing Retina Images</title>
		
		<link href="http://www.ddeville.me/2011/02/a-cocoa-app-for-resizing-retina-images" />
		
		<updated>2011-02-02T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/02/a-cocoa-app-for-resizing-retina-images</id>
		<content type="html">&lt;p&gt;I have added a small Cocoa app call DDRetinaResize to my retina extensions project on GitHub.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;The app is nothing special, it simply takes a folder containing images scaled and named for the retina display as input and populate a destination folder with scaled-down and renamed images.&lt;/p&gt;

&lt;p&gt;Assume you have a folder filled with images named xxx@2x.png or yyy@2x.jpg and scaled for the retina display (so twice the size of the original image). The app will remove the @2x extension to all images and scale them down half. It will then copy the newly created images to your destination folder.&lt;/p&gt;

&lt;p&gt;Again, this is nothing special and the code is far from being nice, but hey, I had a folder of 500 retina images to resize and rename and it only took me 2 seconds ;)&lt;/p&gt;

&lt;p&gt;The project is on GitHub &lt;a href="https://github.com/ddeville/DDRetinaHelpers" title="DDRetinaHelpers"&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Find All Emails in iPhone Address Book</title>
		
		<link href="http://www.ddeville.me/2011/01/find-all-emails-in-iphone-address-book" />
		
		<updated>2011-01-30T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/01/find-all-emails-in-iphone-address-book</id>
		<content type="html">&lt;p&gt;For a project I am currently working on, I had to retrieve all the emails from the user's iPhone address book. The purpose was to later check against the web service database and find whether some of the user's friends already were using the app.&lt;!--more--&gt;
Diving into the AddressBook framework is pretty trivial but since it is all C-based, it can feel unfamiliar to coder mainly used to the Objective-C syntax.&lt;/p&gt;

&lt;p&gt;Following is the code I wrote in order to achieve the previously described.
Note that this code does not check for errors throughout the execution. It is only written in order to roughly describe the method but some checks would definitely have to be added.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="c1"&gt;// we will finally store the emails in an array so we create it here&lt;/span&gt;
&lt;span class="n"&gt;NSMutableArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;emails&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSMutableArray&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
&lt;span class="c1"&gt;// get the address book&lt;/span&gt;
&lt;span class="n"&gt;ABAddressBookRef&lt;/span&gt; &lt;span class="n"&gt;addressBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ABAddressBookCreate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
&lt;span class="c1"&gt;// get an array of all the persons (records) in this address book&lt;/span&gt;
&lt;span class="n"&gt;CFArrayRef&lt;/span&gt; &lt;span class="n"&gt;addressBookRecords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ABAddressBookCopyArrayOfAllPeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addressBook&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
&lt;span class="c1"&gt;// loop through the address book records&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;CFArrayGetCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addressBookRecords&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// get a single person as a record&lt;/span&gt;
    &lt;span class="n"&gt;ABRecordRef&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CFArrayGetValueAtIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addressBookPersons&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// get all email associated with this person&lt;/span&gt;
    &lt;span class="n"&gt;ABMultiValueRef&lt;/span&gt; &lt;span class="n"&gt;emailProperty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ABRecordCopyValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kABPersonEmailProperty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// convert it to an array&lt;/span&gt;
    &lt;span class="n"&gt;CFArrayRef&lt;/span&gt; &lt;span class="n"&gt;allEmails&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ABMultiValueCopyArrayOfAllValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;emailProperty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// add these emails to our initial array&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;emails&lt;/span&gt; &lt;span class="nl"&gt;addObjectsFromArray:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;allEmails&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// release memory&lt;/span&gt;
    &lt;span class="n"&gt;CFRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allEmails&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;CFRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;emailProperty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// release memory&lt;/span&gt;
&lt;span class="n"&gt;CFRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addressBook&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;CFRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addressBookRecords&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// print the elements in the array to check everything worked fine     &lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;emails&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;%@&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Also, make sure you import the Address Book framework by inserting the following line at the top of your header. You will also need to manually add the framework to your project.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#import &amp;lt;AddressBook/AddressBook.h&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
	</entry>
	
	<entry>
		<title>A Mandelbrot Set Visualization on the iPhone</title>
		
		<link href="http://www.ddeville.me/2011/01/a-mandelbrot-set-visualization-on-the-iphone" />
		
		<updated>2011-01-30T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/01/a-mandelbrot-set-visualization-on-the-iphone</id>
		<content type="html">&lt;p&gt;A colleague of mind was reading a book about &lt;a href="http://en.wikipedia.org/wiki/Benoit_Mandelbrot" title="Benoit Mandelbrot on Wikipedia"&gt;Mandelbrot&lt;/a&gt; lately and that reminded me of a coursework I was given at UCL in my introductory course to Java. The purpose was to draw a visualization of the Mandelbrot set in Java. I looked back through my university folder on my Mac to find the coursework. Well, my first impression was: you can write pretty ugly code while a student ;).&lt;!--more--&gt;
But then while reading through my code I thought it would be quite funny to try to re-implement that on the iPhone. Obviously, given the dozens of fractal apps already available on the App Store this would never become an app but it was a funny one so I gave it a go.&lt;/p&gt;

&lt;p&gt;In a nutshell, the Mandelbrot set is a mathematical set of points in the complex plane (the complex plane being a geometric representation of complex numbers on a real and imaginary axis). A complex number &lt;em&gt;c&lt;/em&gt; belongs to the Mandelbrot set if the orbit of 0 under iteration of the complex quadratic iteration $Z_{n+1} = Z_{n}&amp;#94;{2} + c$  remains bounded.
So assuming we have a complex number &lt;em&gt;c&lt;/em&gt; with real part &lt;em&gt;x&lt;/em&gt; and imaginary part &lt;em&gt;y&lt;/em&gt; such that $c = x + iy$, we can conclude that it belongs to the Mandelbrot set if given the following iteration:
$$Z_{0} = c$$
$$Z_{n+1} = Z_{n}&amp;#94;{2} + c$$
&lt;em&gt;Z&lt;/em&gt; never escapes to infinity.
It has been shown that if the magnitude of &lt;em&gt;Z&lt;/em&gt; ever becomes greater than 2, it will eventually escapes to infinity. We thus simply have to compute the magnitude of &lt;em&gt;Z&lt;/em&gt; for a large number of iterations and check whether it goes beyond 2 to conclude it does not belong to the set.
Before going any further, we have to define two properties of complex numbers: how to multiply two complex numbers and how to compute the magnitude of a complex number.
Assuming we have two complex numbers defined by
$$c_{1} = a + ib$$
$$c_{2} = c + id$$
their product is then given by
$$c_{1}c_{2} = (ac-bd) + (bc+ad)i$$&lt;/p&gt;

&lt;p&gt;Thus taking the square of $c_{1}$ would result in
$$c_{1}&amp;#94;{2} = (a&amp;#94;{2}-b&amp;#94;{2}) + 2abi$$&lt;/p&gt;

&lt;p&gt;The magnitude (or absolute value) of a complex number &lt;em&gt;c&lt;/em&gt; is defined as
$$|c| = \sqrt{a&amp;#94;{2}+b&amp;#94;{2}}$$&lt;/p&gt;

&lt;p&gt;Given these 2 definitions, we now simply have to check whether the magnitude for each step of the complex quadratic iteration exceeds 2 (to avoid computing a square root, we can simply check whether the sum of the squares of both real and imaginary parts exceeds 4).&lt;/p&gt;

&lt;p&gt;The C function written to check that is the following&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt; &lt;span class="nf"&gt;isInMandelbrotSet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;nx&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;ny&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;fl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;TRUE&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;MANDELBROT_STEPS&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// We calculate the real part of the sequence&lt;/span&gt;
        &lt;span class="n"&gt;nx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// We calculate the imaginary part of the sequence&lt;/span&gt;
        &lt;span class="n"&gt;ny&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// We compute the magnitude at each step&lt;/span&gt;
        &lt;span class="c1"&gt;// We check if it&amp;#39;s greater than 2&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;nx&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ny&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;fl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;FALSE&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nx&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ny&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fl&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The number of steps is arbitrary but we can obtain accurate results by only taking 50 steps.&lt;/p&gt;

&lt;p&gt;We then have to compute the previous check for each pixel on the frame. In order to get faster computation, I decided to create a bitmap context, draw the Mandelbrot set offscreen and simply create an image from the bitmap context and draw it to the current context in the drawRect: method.&lt;/p&gt;

&lt;p&gt;We first need to create a bitmap context by calling the following method&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CGContextRef&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;createCustomBitmapContextWithSize:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CGSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CGContextRef&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bitmapBytesPerRow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;bitmapBytesPerRow&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;bitmapBytesPerRow&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="n"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;bitmapByteCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapBytesPerRow&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="n"&gt;CGColorSpaceRef&lt;/span&gt; &lt;span class="n"&gt;colorSpace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CGColorSpaceCreateDeviceRGB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;bitmapData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapByteCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapData&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fprintf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Memory not allocated!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CGBitmapContextCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitmapBytesPerRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;colorSpace&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kCGImageAlphaPremultipliedLast&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;free&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;bitmapData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;fprintf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Context not created!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="n"&gt;CGColorSpaceRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colorSpace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The drawing method is then given by the following&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;drawMandelbrot:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CGPoint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt; &lt;span class="nl"&gt;andZoom:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;zoom&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CGContextSetAllowsAntialiasing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;FALSE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="n"&gt;CGContextSetRGBFillColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="n"&gt;CGFloat&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;CGFloat&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// mapping the bounding box to pixels&lt;/span&gt;
    
    &lt;span class="cm"&gt;/*&lt;/span&gt;
&lt;span class="cm"&gt;       zoom 1 has to be between -2 to 1 and -1 to 1&lt;/span&gt;
&lt;span class="cm"&gt;       any additional zoom divides these by the zoom&lt;/span&gt;
&lt;span class="cm"&gt;    */&lt;/span&gt;
    
    &lt;span class="c1"&gt;// loop through every pixel of the frame...&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;480&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;320&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="n"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;1.33f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// -2 to 1 -   screen width  = 480&lt;/span&gt;
            &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="n"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;1.00f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// -1 to 1 -   screen height = 320&lt;/span&gt;
            
            &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;zoom&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;zoom&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isInMandelbrotSet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;CGContextFillRect&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmapContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CGRectMake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I did not have time (or interest..) for implementing various zoom level so we simply draw at the most basic zoom (in the bounding box -2 to 1 on the real axis and -1 to 1 on the imaginary axis).&lt;/p&gt;

&lt;p&gt;The result is quite nice. Again, I did not have much time to implement different colors but it should be trivial to add this feature (the color basically depends on how many steps it takes for the magnitude of &lt;em&gt;Z&lt;/em&gt; in the iteration to exceed 2).&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/01/mandelbrot_set.png" title="Mandelbrot Set" alt="Mandelbrot Set" /&gt;&lt;/p&gt;

&lt;p&gt;By running the drawing code on my iPhone 4 with 50 Mandelbrot steps per pixel, I achieve the following timing (in seconds):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bitmap context creation duration = 0.105825&lt;/li&gt;
&lt;li&gt;drawing Mandelbrot in bitmap duration = 0.684840&lt;/li&gt;
&lt;li&gt;draw rect duration = 0.107269&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This could obviously be improved but for the purpose of the experiment (fun!) I'm quite happy with it!&lt;/p&gt;

&lt;p&gt;I have uploaded the project to &lt;a href="https://github.com/ddeville/Mandelbrot-set-on-iPhone" title="GitHub"&gt;GitHub&lt;/a&gt; so give it a go and have fun ;).&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Getting the Interface Orientation in iOS</title>
		
		<link href="http://www.ddeville.me/2011/01/getting-the-interface-orientation-in-ios" />
		
		<updated>2011-01-27T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/01/getting-the-interface-orientation-in-ios</id>
		<content type="html">&lt;p&gt;One feature that annoys me while developing for the iPad is that no matter how much I take care of coding device orientation correctly, I know that eventually the client will use the device faced-up on his desk while in landscape and come back to me: "the orientation is messed up and displays in portrait while in landscape". (NB: this is also true while coding for the iPhone but you do not really focus that much on multiple orientations)&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;The reason is that while facing-up, UIDevice does not return the interface orientation but the actual device orientation, in this case &lt;em&gt;UIDeviceOrientationFaceUp&lt;/em&gt;. So far so good, but problems arise when you need to switch view controller. Not every element in your view are autoresized and you will need to perform some repositioning based on the interface orientation. Since the device has actually not been rotating (you arrived to this view with the device already been rotating to landscape) the following methods will obviously not been called&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;shouldAutorotateToInterfaceOrientation:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIInterfaceOrientation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;interfaceOrientation&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;willRotateToInterfaceOrientation:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIInterfaceOrientation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;toInterfaceOrientation&lt;/span&gt; &lt;span class="nl"&gt;duration:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSTimeInterval&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;didRotateFromInterfaceOrientation:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIInterfaceOrientation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fromInterfaceOrientation&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The last option you have is to manually check the device orientation by calling&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;UIDevice&lt;/span&gt; &lt;span class="n"&gt;currentDevice&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;orientation&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;And here comes the problem, if the user actually holds the device to a valid orientation, you will be able to map the interface orientation to the device orientation given that&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;UIInterfaceOrientationPortrait&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UIDeviceOrientationPortrait&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UIInterfaceOrientationPortraitUpsideDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UIDeviceOrientationPortraitUpsideDown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UIInterfaceOrientationLandscapeLeft&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UIDeviceOrientationLandscapeRight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UIInterfaceOrientationLandscapeRight&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UIDeviceOrientationLandscapeLeft&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;UIInterfaceOrientation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;But in our case where the device is actually facing up on the desk, the device orientation will not map to any interface orientation and the only option we have is to make an arbitrary decision with 50% chance of being wrong.
This is obviously unacceptable and there might be an easy solution to this very annoying drawback.&lt;/p&gt;

&lt;p&gt;Well, my first thought was to check the application frame by checking&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;UIScreen&lt;/span&gt; &lt;span class="n"&gt;mainScreen&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;applicationFrame&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;but the frame is orientation independent and simply returns the frame in portrait mode.&lt;/p&gt;

&lt;p&gt;The method that seems to actually do the job is to check the status bar orientation. Indeed, if you do not try to manually set its orientation, the status bar gently follows the orientation of the screen and thankfully its orientation is directly available by getting&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;UIApplication&lt;/span&gt; &lt;span class="n"&gt;sharedApplication&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;statusBarOrientation&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This way, you can get the interface orientation at any time by simply checking this property. Note that it does not matter whether the status bar is hidden or not, the property will be updated no matter what.&lt;/p&gt;

&lt;p&gt;Hope that helps! ;)&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>DDPageControl, a Custom UIPageControl</title>
		
		<link href="http://www.ddeville.me/2011/01/ddpagecontrol-a-custom-uipagecontrol" />
		
		<updated>2011-01-15T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/01/ddpagecontrol-a-custom-uipagecontrol</id>
		<content type="html">&lt;p&gt;UIPageControl, the sibling of any UIScrollView with paging enabled is a widely used control (Apple uses it in many of their apps, Weather, Safari, even to scroll between screens in iOS).&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;However, when building an app, designers are rarely happy with the standard UIPageControl and often prefer a slight variant of it (changing the color of the indicators, their size, etc..).
Unfortunately, UIPageControl is not customizable at all and you have to stick with the 4px-diameter indicators, 12px-spacing, white-on and gray-off if you want to use it.&lt;/p&gt;

&lt;p&gt;Building your own page control is however not hard and that's what I decided to do: here comes DDPageControl!
My first requirement was to replicate exactly the features of UIPageControl and add some additional properties for its customization. This way, you can easily replace any existent UIPageControl with a DDPageControl and it will work out of the box without any change. Obviously, if you do not set any additional properties, you will have the same look and feel as the original UIPageControl.
However, DDPageControl adds the following properties:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;retain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;onColor&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;retain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;offColor&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;CGFloat&lt;/span&gt; &lt;span class="n"&gt;indicatorDiameter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;CGFloat&lt;/span&gt; &lt;span class="n"&gt;indicatorSpace&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@property&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonatomic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;DDPageControlType&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This way, you can easily customize the color of the indicators by setting the properties &lt;em&gt;onColor&lt;/em&gt; and &lt;em&gt;offColor&lt;/em&gt;. &lt;em&gt;onColor&lt;/em&gt; is the color of a single dot representing the current page and &lt;em&gt;offColor&lt;/em&gt; are all other dots. You can obviously also pass an alpha channel in your UIColor giving the opportunity to achieve a very nice look!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;indicatorDiameter&lt;/em&gt; is a property you can set to define the size of any dot. &lt;em&gt;indicatorSpace&lt;/em&gt; is the spacing between two dots.&lt;/p&gt;

&lt;p&gt;Eventually, the &lt;em&gt;type&lt;/em&gt; property can be any of the following defined in the enum:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;DDPageControlTypeOnFullOffFull&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;DDPageControlTypeOnFullOffEmpty&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;DDPageControlTypeOnEmptyOffFull&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;DDPageControlTypeOnEmptyOffEmpty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;DDPageControlType&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;It defines the general look of &lt;em&gt;on&lt;/em&gt; and &lt;em&gt;off&lt;/em&gt; dots: full or empty.&lt;/p&gt;

&lt;p&gt;The code can be found on GitHub in my repository: &lt;a href="https://github.com/ddeville/DDPageControl" title="DDPageControl"&gt;DDPageControl&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;DDPageControl&lt;/em&gt; class is the one you want to import in your project to use it. However, &lt;em&gt;DDPageControlViewController&lt;/em&gt; can also be helpful since it gives an example of how to use the page control along with a classic &lt;em&gt;UIScrollView&lt;/em&gt;, above all how to set the current page in your &lt;em&gt;scrollViewDidScroll&lt;/em&gt; and &lt;em&gt;scrollViewDidEndScrollingAnimation&lt;/em&gt; methods. In particular, using &lt;em&gt;defersCurrentPageDisplay&lt;/em&gt; set to YES and calling &lt;em&gt;updateCurrentPageDisplay&lt;/em&gt; once the animation is terminated is an optimal way to obtain a smooth transition of the page control when animated the &lt;em&gt;UIScrollView&lt;/em&gt;. This not only apply to &lt;em&gt;DDPageControl&lt;/em&gt; but more generally to any &lt;em&gt;UIPageControl&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Following are some example of what you can obtain by setting the properties as stated.&lt;/p&gt;

&lt;p&gt;Screen &lt;em&gt;D&lt;/em&gt; was obtained with the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setType:&lt;/span&gt; &lt;span class="n"&gt;DDPageControlTypeOnFullOffEmpty&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setOnColor:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithWhite:&lt;/span&gt; &lt;span class="mf"&gt;0.9f&lt;/span&gt; &lt;span class="nl"&gt;alpha:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setOffColor:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithWhite:&lt;/span&gt; &lt;span class="mf"&gt;0.7f&lt;/span&gt; &lt;span class="nl"&gt;alpha:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setIndicatorDiameter:&lt;/span&gt; &lt;span class="mf"&gt;15.0f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setIndicatorSpace:&lt;/span&gt; &lt;span class="mf"&gt;15.0f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/01/ddpagecontrol_d.png" title="DDPageControl" alt="DDPageControl" /&gt;&lt;/p&gt;

&lt;p&gt;Screen &lt;em&gt;E&lt;/em&gt; was obtained with the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setType:&lt;/span&gt; &lt;span class="n"&gt;DDPageControlTypeOnEmptyOffFull&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setOnColor:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithWhite:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="nl"&gt;alpha:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setOffColor:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithWhite:&lt;/span&gt; &lt;span class="mf"&gt;0.7f&lt;/span&gt; &lt;span class="nl"&gt;alpha:&lt;/span&gt; &lt;span class="mf"&gt;0.5f&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setIndicatorDiameter:&lt;/span&gt; &lt;span class="mf"&gt;10.0f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setIndicatorSpace:&lt;/span&gt; &lt;span class="mf"&gt;15.0f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/01/ddpagecontrol_e.png" title="DDPageControl" alt="DDPageControl" /&gt;&lt;/p&gt;

&lt;p&gt;Screen &lt;em&gt;F&lt;/em&gt; was obtained with the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setType:&lt;/span&gt; &lt;span class="n"&gt;DDPageControlTypeOnFullOffFull&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setOnColor:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithWhite:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="nl"&gt;alpha:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setOffColor:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithWhite:&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="nl"&gt;alpha:&lt;/span&gt; &lt;span class="mf"&gt;0.5f&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setIndicatorDiameter:&lt;/span&gt; &lt;span class="mf"&gt;13.0f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pageControl&lt;/span&gt; &lt;span class="nl"&gt;setIndicatorSpace:&lt;/span&gt; &lt;span class="mf"&gt;10.0f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2011/01/ddpagecontrol_f.png" title="DDPageControl" alt="DDPageControl" /&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>iOS Devices Capability Detection</title>
		
		<link href="http://www.ddeville.me/2011/01/ios-devices-capability-detection" />
		
		<updated>2011-01-13T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2011/01/ios-devices-capability-detection</id>
		<content type="html">&lt;p&gt;I have written a small class that will make your life easier if you need to deploy iOS apps across different versions of the OS and multiple device generations.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;You might have already written some code like that to check if the device is running iOS 4.0 and has thus multitasking:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kCFCoreFoundationVersionNumber&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;kCFCoreFoundationVersionNumber_iOS_4_0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;YES&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;NO&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;While this is valid, it might just be annoying to have to rewrite it every time you need to check it!
&lt;em&gt;DDDeviceDetection&lt;/em&gt; defines simple class method such as &lt;em&gt;isRunningiOS4&lt;/em&gt; that you can easily call anywhere through your code, assuming you first import the header in your class.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="cp"&gt;#import &amp;quot;DDDeviceDetection.h&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;DDDeviceDetection&lt;/span&gt; &lt;span class="n"&gt;isRunningiOS4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="c1"&gt;// do something that only runs on iOS4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;There are many methods checking various hardware and software capabilities such as the OS version, device type, camera, compass, gyroscope, geo-location, audio and video.&lt;/p&gt;

&lt;p&gt;You can find the code in my GitHub repository &lt;a href="https://github.com/ddeville/DDDeviceDetection" title="DDDeviceDetection"&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Version Control with Git</title>
		
		<link href="http://www.ddeville.me/2010/12/version-control-with-git" />
		
		<updated>2010-12-29T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2010/12/version-control-with-git</id>
		<content type="html">&lt;p&gt;I have been reading the book "Version control with Git" by Jon Loeliger during the last two days.
I have to admit it is excellent. Git is a joy to work with (even more if one has had experience with another revision control system such as Subversion) and reading this book will unveil so many secrets and tips about Git that will make your developer life much easier.&lt;!--more--&gt;
Also, the author really knows his stuff and keep the reader focused and interested. I managed to read the book cover to cover in less than two days which is, given the 'dryness' of the topic, quite remarkable!&lt;/p&gt;

&lt;p&gt;The book is available on &lt;a href="http://www.amazon.co.uk/Version-Control-Git-collaborative-development/dp/0596520123" title="Amazon"&gt;Amazon&lt;/a&gt; and I also wrote a comment about it that you can find &lt;a href="http://www.amazon.co.uk/review/R3268HL6DTT59E/ref=cm_cr_dp_perm?ie=UTF8&amp;amp;ASIN=0596520123&amp;amp;nodeID=266239&amp;amp;tag=&amp;amp;linkCode=" title="Amazon"&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>LibraryThing iOS Developers Group</title>
		
		<link href="http://www.ddeville.me/2010/12/librarything-ios-developers-group" />
		
		<updated>2010-12-28T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2010/12/librarything-ios-developers-group</id>
		<content type="html">&lt;p&gt;I love books. Actually, I love textbooks. There is nothing like the feeling of studying through the pages of a good textbook. But, as a real nerd, I also love computers.&lt;!--more--&gt;
Here comes the dilemma: I like paper books too much to envisaging keeping my library as Kindles (or iBooks). However, while on the go, I like to be able to browse and contemplate my library without having to bring ten boxes of books!&lt;/p&gt;

&lt;p&gt;For all book lovers, there is an amazing website that allows you to do just that: LibraryThing.
LibraryThing allows you to keep a virtual collection of all your books, classified by category, tags (and many more things like a forum where you can discuss your beloved novels with other users, groups where user with similar taste can exchange tips, review books, etc...).&lt;/p&gt;

&lt;p&gt;I have created a group for iOS and Mac OS X developers. I have also inserted a first topic: "Best iOS development books". The URL to the group is the following: &lt;a href="http://www.librarything.com/groups/iosandmacosxdevelope" title="Link"&gt;http://www.librarything.com/groups/iosandmacosxdevelope&lt;/a&gt;
(yes, there is no 'r' in developer, the URL has been generated by LibraryThing like that, ugly I know...)&lt;/p&gt;

&lt;p&gt;You can also find my profile on LibraryThing at:
&lt;a href="http://www.librarything.com/profile/dd2284" title="Link"&gt;http://www.librarything.com/profile/dd2284&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>UIColor with Pattern Image</title>
		
		<link href="http://www.ddeville.me/2010/12/uicolor-with-pattern-image" />
		
		<updated>2010-12-27T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2010/12/uicolor-with-pattern-image</id>
		<content type="html">&lt;p&gt;A very handy method but not often referenced method in UIColor is&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;colorWithPatternImage:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIImage&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;!--more--&gt;


&lt;p&gt;Very often, you find yourself having to add a background element to your UIViews. Such background are almost always a plain image with repetitive elements (such as a chess board for example). Having to insert a plain 640x960 (or more if using a scroll view) image to you view and project can be a complete waste of memory.
In these cases, you might consider using a UIColor and simply instantiate it with a pattern image through colorWithPatternImage.&lt;/p&gt;

&lt;p&gt;Assume you want to achieve the following effect in your UIView:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2010/12/uicolor_pattern_iphone.png" title="Chess Board Background iPhone" alt="Chess Board Background iPhone" /&gt;&lt;/p&gt;

&lt;p&gt;Instead of using a plain 640x960 UIImage, consider using the following (small!) image and line of code:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2010/12/uicolor_pattern_background.png" title="background" alt="background" /&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;viewDidLoad&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;super&lt;/span&gt; &lt;span class="n"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIColor&lt;/span&gt; &lt;span class="nl"&gt;colorWithPatternImage:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UIImage&lt;/span&gt; &lt;span class="nl"&gt;imageNamed:&lt;/span&gt; &lt;span class="s"&gt;@&amp;quot;background.png&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="nl"&gt;setBackgroundColor:&lt;/span&gt; &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This way, you only need to load in memory the small pattern image and the UIColor will do the job of extending it to fill the background for you.
Also, you do not have to worry about positioning and resizing the background UIImageView to fit your view.&lt;/p&gt;

&lt;p&gt;A small tip to bear in mind (from Apple documentation):&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="html"&gt;By default, the phase of the returned color is 0, which causes the top-left corner of the image to be aligned with the drawing origin. To change the phase, make the color the current color and then use the CGContextSetPatternPhase function to change the phase.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
	</entry>
	
	<entry>
		<title>Lua Tables Under the Hood</title>
		
		<link href="http://www.ddeville.me/2010/11/lua-tables-under-the-hood" />
		
		<updated>2010-11-28T00:00:00+00:00</updated>
		<id>http://www.ddeville.me/2010/11/lua-tables-under-the-hood</id>
		<content type="html">&lt;p&gt;I have been playing with Lua lately and I have to say that I'm pretty amazed by the simplicity and efficiency of the language!&lt;!--more--&gt;
I am particularly amazed by the way they define data structures for example by treating everything as HashTables (tables in Lua). Until Lua 5.0, every data structures was literally a strict HashTable. Since 5.0, they introduced the new "hybrid" table with every table containing a hash part and an array part. The reason for that is in case you want to store a pair with an integer key, you do not really need the overload of the hashing process when an array would do the job and allow you to store and retrieve the value with a single lookup.
However, for avoiding waste of memory, a pair with an integer key n is inserted to the array part of the tables only if at least half the slots between 1 and n are occupied. In that way, a table would not have to create a 1,000,000 empty slots array in case the first key that I am trying to insert is 1,000,000. It would be added to the hash part of the table and moved to the array part if needed later on since the size and structure of the table is recomputed every time the table needs to grow.
This is obviously a low-level implementation completely transparent to the user (and even to the virtual machine). The only thing you actually see when using Lua are tables.&lt;/p&gt;

&lt;p&gt;A summary of the features and implementation in Lua 5.0 can be found in this &lt;a href="http://www.lua.org/doc/jucs05.pdf" title="Paper"&gt;paper&lt;/a&gt; by the authors of the language.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Bitshift Operator and Bit Counting</title>
		
		<link href="http://www.ddeville.me/2010/10/bitshift-operator-and-bit-counting" />
		
		<updated>2010-10-30T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2010/10/bitshift-operator-and-bit-counting</id>
		<content type="html">&lt;p&gt;One subject I've always found a bit tricky (in particular during interviews, since it seems to be a &lt;em&gt;very&lt;/em&gt; common question) is bit representation and bit counting. This is a rather simple stuff, even if, as we will see, it can be quite tricky if we want to go beyond the naive method.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;To get into the subject, let's assume I am interviewing you and that I ask you to write a function that returns the number of pairs of set bits 1 in a given 32-bit integer.
Ok, so now you're thinking: well, I have a function that takes an integer argument, easy, I simply need to loop through the bits, determine if the previous bit for any 1 was also 1 and increment my counter by one, that's it!
But wait, how will I get this bit representation of my integer??&lt;/p&gt;

&lt;p&gt;Here comes the bitshift operator to the rescue. The bitshift operators &gt;&gt; and &amp;lt;&amp;lt; are defined in almost every programming language and basically ... shift the bits!
Let's assume you have the following bit pattern: 0100110. By applying &amp;lt;&amp;lt; 1 to this number will shift the bits one position to the left and lead to 1001100. Similarly, applying &gt;&gt; 1 would give us 0010011. Got it?
(Before going on, it's important to note that the bitshift operator does &lt;em&gt;not&lt;/em&gt; actually modify the given number. If you want to modify the number, you should use &gt;&gt;= and &amp;lt;&amp;lt;= instead. It is the same distinction as applying + and += to a number).&lt;/p&gt;

&lt;p&gt;So, you can actually multiply an int by 2 by simply bit shifting once to the left. Obviously, the number has to be smaller than $2&amp;#94;{31}$ if we want to be able to shift bits once to the left. In case there is a 1 as the leftmost bit of the binary representation, it will simply be discarded. Similarly, the first number from the right will be filled with a 0. This pattern can change in case we are dealing with signed numbers but for the sake of simplicity, we will assume that we are dealing with unsigned integers here.&lt;/p&gt;

&lt;p&gt;So, bit shifting the following 32-bit integer $2&amp;#94;{31}$&lt;br/&gt;
10000000000000000000000000000000&lt;br/&gt;
will result in&lt;br/&gt;
00000000000000000000000000000000&lt;br/&gt;
which is simply 0.&lt;/p&gt;

&lt;p&gt;The reason is that the largest number that one can represent with a 32-bit integer is $2&amp;#94;{32}-1$ or 4294967295 in case 10. By shifting bits to the left by one position for $2&amp;#94;{31}$, we actually multiply the integer by 2 and obtain $2&amp;#94;{32}$ which is to large to be represented with a 32-bit integer.&lt;/p&gt;

&lt;p&gt;Alright, so first of all, let's introduce the naive method for bit counting.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="c1"&gt;// function that counts the number of 1 in a given integer&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;numberOfOnes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This method will indeed return the number of set bits in a given integer. However, it has to loop n times, n being the position of the left-most set bit in the integer. So, in the worst-case scenario, $2&amp;#94;{31}$, we will have to loop 32 times in order to reach the only bit set in this integer!
Let's analyze what is happening in this for loop.
We first check whether the rightmost bit is 1 by bitwise comparing the integer with 0x1 (hexadecimal representation of 1). This returns another integer where each bit is set to 1 only if the corresponding bit in both number is 1.&lt;/p&gt;

&lt;p&gt;For example, assuming we input the number 13:&lt;br/&gt;
001101  --&gt; 13 in base 10&lt;br/&gt;
000001  --&gt; 1 in base 10&lt;br/&gt;
gives&lt;br/&gt;
000001  --&gt; 1 in base 10&lt;/p&gt;

&lt;p&gt;but inputing 14:&lt;br/&gt;
001110  --&gt; 14 in base 10&lt;br/&gt;
000001  --&gt; 1 in base 10&lt;br/&gt;
gives&lt;br/&gt;
000000  --&gt; 0 in base 10&lt;/p&gt;

&lt;p&gt;So basically, "num &amp;amp; 0x1" returns 1 if num has a 1 on the rightmost bit and 0 if it has a 0.
By incrementing the counter variable by this value, we simply add 1 every time we come across a 1 in the integer and simply add 0 otherwise.
So far, we understood how to detect whether the rightmost bit is 1 or 0 but we have to loop through every bit in that integer. We could easily compare the integer with all powers of two until $2&amp;#94;{31}$ but we do not really want to have to loop 32 times every time, even if the number to check is 1 or 2. We would rather loop through the integer we input until we reach the leftmost bit that is set. To do that, we use the fact that we can easily right shift all bits in the integer and we keep doing that until no 1 is left in the integer (resulting in an integer of value 0 in base 10).
Hence, we keep right shifting the integer by one position until it results in 0.&lt;/p&gt;

&lt;p&gt;Similarly, we can count the number of pairs of 1 in a given integer by using the following function.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;numberOfOnePairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;lastWasOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;false&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastWasOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;lastWasOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="n"&gt;lastWasOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;false&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;A handy tool in order to understand better what's happening here is another function that simply prints the binary representation of any unsigned integer.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printBitRepresentation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// sizeof returns bytes, so need to multiply by 8 to get bits&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;1&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;0&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;\0&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In another post, I'll talk about methods that go beyond the naive method and do not require to loop through every bit.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Sorting Algorithms Comparison</title>
		
		<link href="http://www.ddeville.me/2010/10/sorting-algorithms-comparison" />
		
		<updated>2010-10-24T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2010/10/sorting-algorithms-comparison</id>
		<content type="html">&lt;p&gt;I have been working on sorting algorithms during the last week and I thought it would be worth writing a small article about it.&lt;!--more--&gt;
Trying to sort an array "brute-force" in any programming language would almost always take ages and could lead to memory overflows.
Fortunately, many efficient sorting algorithms have been designed. However, not every algorithm is as efficient and each algorithm performs better or worse in some given situations.
As a starting point, let's first illustrate the complexity of five famous algorithms (I am not a big fan of Bubble Sort but it's also worth to show bad examples in order to fully illustrate a topic):&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2010/10/algo_table_1.png" title="Table 1" alt="Table 1" /&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, in order to illustrate better the overall complexity of each algorithm, let's look at the running time of each algorithm for various array sizes in three very different situations:
- when the array is already sorted (which leads to best case scenario for some algorithms but as we will see, not all of them).
- when the array is already sorted but in reverse order (leads to worst case scenario for simple algorithms).
- when the array is randomly ordered.&lt;/p&gt;

&lt;p&gt;Sorted array:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2010/10/algo_table_2.png" title="Table 2" alt="Table 2" /&gt;&lt;/p&gt;

&lt;p&gt;Reverse sorted array:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2010/10/algo_table_3.png" title="Table 3" alt="Table 3" /&gt;&lt;/p&gt;

&lt;p&gt;Randomly sorted array:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.ddeville.me/static/images/posts/2010/10/algo_table_4.png" title="Table 4" alt="Table 4" /&gt;&lt;/p&gt;

&lt;p&gt;We randomly sorted the array by generating its elements with the following routine:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arraySize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;arraySize&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;srandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;(Note that '-' simply means that I did not have the patience to wait for the algorithm to return... You get the idea anyway).&lt;/p&gt;

&lt;p&gt;The first comment we can make out of this data is that Merge Sort performs indeed quite well on average and does not give bad results even in the worst case.
However, for each situation it is outperformed by another algorithm, Bubble and Insertion Sorts when the array is already sorted and Quick Sort when the array is randomly sorted.
The reason is that when the array is already sorted, simple algorithms such as Bubble Sort and Insertion Sort reach their best case scenario (but reaching their worst case scenario if the array is sorted in reverse order).
Furthermore, even if Quick Sort and Merge Sort have same complexity on average and in best case scenario, the constants (hidden by the big-O notation) are much smaller in Quick Sort leading to a smaller running time on average. However, the pitfall is that Quick Sort performs bad in the worst case scenario so it can be quite of a risky alternative.&lt;/p&gt;

&lt;p&gt;So, even if we can consider Merge Sort a "safe alternative" that guarantee good results in any case, it is very often simply not the best option.
The reason is mainly that the worst-case scenario is after all really rare and believe it or not, half of the time you try to sort an array, it is actually already sorted. In that case, Merge Sort will still perform in $\mathcal{O}(n\log{n})$ while Insertion Sort for instance will perform in $\mathcal{O}(n)$.&lt;/p&gt;

&lt;p&gt;So my piece of advice is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;analyse the problem carefully before deciding the sorting algorithm.&lt;/li&gt;
&lt;li&gt;often, for small arrays or arrays that are already (or almost) sorted, Insertion Sorting will do the job and even perform better than a more complex algorithm.&lt;/li&gt;
&lt;li&gt;if you care about memory, avoid Merge Sort since it basically uses loads of temporary arrays that will easily overflow your memory.&lt;/li&gt;
&lt;li&gt;even if Quick Sort has a $\mathcal{O}(n&amp;#94;{2})$ worst case, remember that worst case is often rare and Quick Sort will perform much better that Merge Sort on average since its constants (hidden by the big-O notation) are actually much smaller.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Following is a C implementation of the five algorithms.&lt;/p&gt;

&lt;p&gt;BUBBLE SORT&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;bubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// loop through every item in the array&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// loop a second time from the back of the array&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// swap the elements if necessary&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;INSERTION SORT&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;insertionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// we loop through all elements in the original array from the second element&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// store the current element as the key&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// get the element just before the current element&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// loop through all elements from the key to the start&lt;/span&gt;
        &lt;span class="c1"&gt;// check if the current element is smaller than the key&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// we move the current element backward&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// we finally move the key&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;MERGE SORT&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// prerequisite&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// get the middle point&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
        &lt;span class="c1"&gt;// apply merge sort to both parts of this&lt;/span&gt;
        &lt;span class="n"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
        &lt;span class="c1"&gt;// and finally merge all that sorted stuff&lt;/span&gt;
        &lt;span class="n"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;firstIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secondIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;tempArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// if there are still objects in both arrays&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;firstIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;firstIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;secondIndex&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;tempArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;firstIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;firstIndex&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;tempArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;secondIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;secondIndex&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="c1"&gt;// terminates the object of the lower array&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tempArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;firstIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;firstIndex&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="c1"&gt;// terminates the object of the upper array&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tempArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;secondIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;secondIndex&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="c1"&gt;// transfer to the initial array&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tempArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;HEAP SORT&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="c1"&gt;// returns the left node (by doubling the current node)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;leftNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// this actually does 2*node&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// returns the right node (by doubline the current node and adding 1)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;rightNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// this actually does 2*node+1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// return the parent node (by taking the half of the&lt;/span&gt;
&lt;span class="c1"&gt;// current node and returning its floor)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// restore the heap property&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;maxHeapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// get the two children nodes&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leftNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rightNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// assume that the largest is originally the current node&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// check if the left node is the largest&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// check if the right node is the largest&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;largest&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// in case the left or right node was larger than the parent&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// we switch the parent with the largest child&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;largest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;largest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
        &lt;span class="c1"&gt;// and apply maxHeapify recursively to the subtree&lt;/span&gt;
        &lt;span class="n"&gt;maxHeapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;largest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// build the heap by looping through the array&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;buildMaxHeap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;maxHeapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;heapSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arraySize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// determine the heap size&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arraySize&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// build the heap&lt;/span&gt;
    &lt;span class="n"&gt;buildMaxHeap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="c1"&gt;// loop through the heap&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// swap the root of the heap with the last element of the heap&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
        &lt;span class="c1"&gt;// decrease the size of the heap by one so that the previous&lt;/span&gt;
        &lt;span class="c1"&gt;// max value will stay in its proper placement&lt;/span&gt;
        &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        
        &lt;span class="c1"&gt;// put the heap back in max-heap order&lt;/span&gt;
        &lt;span class="n"&gt;maxHeapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;heapSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;QUICKSORT&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="objc"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// define a pivot as the max item of the (sub)array&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// loop through the elements of the (sub)array&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// in case the element has a smaller value that the pivot&lt;/span&gt;
        &lt;span class="c1"&gt;// bring it in front of it (larger elements will come after it)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// bring the pivot to its correct position&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// partition the array in two parts&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// apply QuickSort recursively to both parts&lt;/span&gt;
        &lt;span class="n"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
	</entry>
	
	<entry>
		<title>The Fate of Languages in Academics</title>
		
		<link href="http://www.ddeville.me/2010/10/the-fate-of-languages-in-academics" />
		
		<updated>2010-10-21T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2010/10/the-fate-of-languages-in-academics</id>
		<content type="html">&lt;p&gt;I came across this very good article about the fate of languages in universities from the UCL Vice Provost Michael Worton on the Times Higher Education &lt;a href="http://www.timeshighereducation.co.uk/story.asp?sectioncode=26&amp;amp;storycode=413898&amp;amp;c=2" title="THE"&gt;website&lt;/a&gt;.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;I am really happy to see that universities (and in particular UCL considering it is the university I studied in) are actually trying to make steps forward in this area.
My idea is that even if the access to languages is definitely much easier for students nowadays, through all sort of academic exchanges (Erasmus, Socrates, you name it), there is no reason why proper academic teaching/research should be put away. Nowadays, almost every student has spent at least an academic year abroad, at least in Europe. I studied in four countries and I would do the same exact thing if I had to do it again. Throughout, I've always considered the learning of the language a huge part of my discovering and integration to the country I was living in. The process of learning a language is maybe the best gate I could imagine of in trying to understand a culture and its people. For this reason, I never did and will never agree that treating English as a de facto standard, giving it such a predominance in teaching versus any other language, will make ourselves global citizens. International English is a great tool that makes communications among people easier "at first sight" but from my experience, it will never be a substitute to properly learning the language of the person you are communicating with...
And this is only possible if universities keep doing their great job in teaching millions of students languages and studying every single details about languages and discovering all these little tricks and weirdnesses, sentiment of déjà-vu or complete "lost in translation" feeling, that make the study of a language such a fantastic journey!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>iPhone Retina Extensions</title>
		
		<link href="http://www.ddeville.me/2010/10/iphone-retina-extensions" />
		
		<updated>2010-10-20T00:00:00+01:00</updated>
		<id>http://www.ddeville.me/2010/10/iphone-retina-extensions</id>
		<content type="html">&lt;p&gt;I started to write a couple of Cocoa applications to play with file extensions and image sizes.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;The reason for that is that, often, designers only give you the larger version (the one used for devices with a retina display) of an image and don't add the @2x extension to the file name.
I very often found myself renaming a whole bunch of files, opening each of them with Preview, resizing them and changing the name back.
This is a tedious process that unfortunately takes a developer off his main task and well, let's face it, wastes his time!&lt;/p&gt;

&lt;p&gt;The first application is a command-line application that simply add the @2x extension to all the images (.png .jpg and .jpeg) contained in a folder and copy them in another folder (this one can of course be the same as the original one).&lt;/p&gt;

&lt;p&gt;The project is named DDRetinaExtension and you can find the source code on &lt;a href="http://github.com/ddeville/DDRetinaHelpers" title="GitHub"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Simply build the project, cd to the folder where the project has been built and run the following command:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /folder/where/the/built/resides
&lt;span class="nv"&gt;$ &lt;/span&gt;./RetinaExtension --from /origin/path/ --to /destination/path/
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;And that's basically it! This will rename all images located in the origin path, add the @2x extension and copy them to the destination path.&lt;/p&gt;

&lt;p&gt;I am currently working on a proper Cocoa application that will also integrate the resizing and make this process a bit more friendly!&lt;/p&gt;
</content>
	</entry>
	
</feed>

