Cocoa Crunch (Day 1)

Cocoa Programming for Mac OS X: five chapters a day, for one week

It would be fair to say I’ve gotten a wee-bit rusty with my Objective-C / Cocoa skills since being laid off last summer. Life, as it so often does, simply interceded for a while and demanded a good portion of my attention. Time to get back in the Cocoa saddle, and I’m approaching this as a kind of “sprint”, time-boxed to one week..

I attended Big Nerd Ranch about five years ago and had an amazing week of learning and discovery. If you haven’t been, I recommend it wholeheartedly. Since then, I’ve done the exercises and read Hillegass’ book “Cocoa Programming for Mac OS X” so many times I’ve lost count. But it remains the definitive way to learn Cocoa programming (in my opinion) and is also outstanding refresher for those who need a kickstart.

This is a log of what I’ve encountered during this crunch time, tackling five chapters a day for one week. My Scrum definition of “done” for this project means:

  • All exercises are complete, including all “Challenges”.
  • All code compiles with zero errors and zero warnings.
  • All code compiles in both 10.5 and 10.6 SDKs.
  • All code is fully and professionally commented in my own words to show that the intent of the lesson has been internalized.
  • All code works as described in the book.
  • One extra feature is introduced to each project (GUI widget, re-factored code, etc.).
  • Deprecated method calls are researched, rewritten, and recoded; physical book is likewise updated.

Switching to OS X 10.6 with Xcode 3.2.1 turned out to have surprises in store for this Hillegass veteran.

The default template for a Cocoa application has changed

Aaron mentions this briefly on his website, but I will mention it again in relation to my own studies. The default template now includes a pre-wired “AppDelegate” which conforms to the 10.6 <NSApplicationDelegate> protocol. This is all well and good, especially for iPhone programmers switching over to OS X desktop. However, I wish to target 10.5 users as well with file_wrangler_2 and this change threw me for a small loop when I compiled for 10.5 and couldn’t get a simple blank window to open. The simplest possible program wasn’t working and I immediately felt a slight wave of panic. “Have I been away for TOO LONG?” I thought. No, it was simple to understand. The <NSApplicationDelegate> protocol doesn’t exist in 10.5, so just remove that and one can continue along neat-as-you-please. Alternately, delete the auto-created AppDelegate classes and delete the instance in Interface Builder, then just follow along. I’ve opted to use the AppDelegate in place of the AppController classes Aaron makes throughout the book.

Clang LLVM 1.0 is wonderful

I started tinkering with Clang LLVM after hearing about it during the Stanford iPhone programming series on iTunesU. It had so much potential, but was a little kludgy to use. Its not kludgy anymore, and I’m very much in love with it. It turns this message about a missing semicolon:

into this:


It also showed me a couple of potential leaks, understanding that I had done alloc/init without corresponding release statements. A pure joy to behold!

Deprecated methods

I was more than a little surprised to see my old standby, NSCalendarDate, is deprecated. Now, rather than creating an NSCalendarDate and doing something like dateByAddingYears:months:days:hours:minutes:seconds to get a new NSCalendarDate object at relative point in the past/future, we must use an NSDate and add an NSTimeInterval (in seconds) to it. According to Apple’s sample code, this means calculating the number of seconds manually, in this manner:
NSUInteger secondsPerHour = 60 * 60;
NSUInteger secondsPerDay = secondsPerHour * 24;
NSUInteger secondsPerWeek = secondsPerDay * 7;
NSUInteger secondsPerMonth = (get the number of days for a particular month) * secondsPerDay;

…and so forth. I suppose some #define macros are now in order. Whenever possible I am trying to switch the sample code to non-deprecated methods when the replacement method (or class) is available in both 10.5. and 10.6.

Considering 64-bit

Basically this just means using NSInteger in place of int to keep both 32-bit and 64-bit processors as happy as possible. Not a problem, just a mental shift and a point I don’t see mentioned in the book.

The first five chapters

Once I got past the new documentation browser of Xcode, the new Cocoa template of Xcode 3.2, understood how nicely Clang LLVM has been integrated, and tweaked my use of the new Interface Builder a bit, I was able to complete chapters 1-5 with increasing speed toward the end of the evening. Now, I simply must stop blogging and get to work on chapters 6-10.

Leave a Reply

Your email address will not be published. Required fields are marked *