A Review of Cocoa Programming for Mac OS X
Comments on the 2nd Edition in green


Abstract: Yes, this book came out about a year and a half ago, and I even read it last year, but I've been meaning to write up a review of it for some time. There are still a good number of beginning Cocoa programmers out there looking for more information on where to start. This is a small contribution to that cause.


Writing Cocoa applications on the Mac is a joy born from the acquisition (/reintegration/merger) of NeXT some years ago, when Apple was shopping around for a new operating system. Cocoa is the evolution of the programming framework created by NeXT, and so has been in steady development (under various names) for over 15 years. On the Mac, it is one of the two main frameworks, and currently accounts for roughly 30% of all applications on OS X (a figure that's ever so slowly climbing). It's based on the Objective-C language, an object-oriented extension of C based on the Smalltalk model of OO language design. There are other languages one can use with Cocoa, but as the framework itself was written with Objective-C, this is its natural language.

To date, there have been several good books written on Cocoa programming, as well as a long list of articles on the net. However, though each programmer responds to different teaching techniques, it was within the first four chapter's of Aaron Hillegass's book, Cocoa Programming for Mac OS X that I finally found myself writing my own programs which were no longer but minor variations of cookbook examples. As well, this is still the first book I find myself reaching for first when having issues getting the fundamental structures of my programs right (as opposed to specific help about certain aspects of the Cocoa framework). By the time I finished the book, I had created about 20 small applications, many of which I use everyday.

Aaron himself is an old-time NeXT programmer and has a great deal of experience with the most effective design strategies in (what is now) Cocoa. He runs a programming retreat, the Big Nerd Ranch, which teaches Cocoa programming, in addition to other topics. It's also non-trivial that Aaron is a very friendly guy, and this comes out in his writing style.

if (you're coming to Objective-C/Cocoa from a non-C/C++ background) {

} else { }

Ok, back to the book. The structure, per se, is similar to many Cocoa books. It starts with a few introductory chapters: what is Cocoa; an introduction to using Project Builder and Interface Builder; and, the basics of Objective-C. From there the real content of how to become a Cocoa programmer is coupled with task-oriented chapters which work through enough of the framework to get you started creating your own projects while you follow along in the book.

The way each chapter is shaped is one thing I really liked about this book. Several chapters begin with a short personal tale which gives you the bigger picture about the particular topic. This adds a nice tone to the book. Aaron then gives an overview of the objects in the framework being addressed in the chapter, along with the methods for those objects which are important to get to know first. This often includes one of my favorite features of this book: object and relationship diagrams which make very clear the relationship between the objects involved, their parent classes, how parts of actual programs interact, etc. I'm a fairly visual person, and these diagrams really help me get clear the important concepts and not get bogged down in syntactical issues. Good diagrams mixed with screen shots of the elements of Interface Builder and inter-object communication helps the reader feel confident about what's going on behind the scenes.

Book Length: 383 pages (1st ed.),
450 pages (2nd ed.)

Chapter Titles:
  • Cocoa: What Is It?
  • Let's Get Started
  • Objective-C
  • Controls
  • Helper Objects
  • Bindings and NSController
  • NSUndoManager
  • Archiving
  • Nib Files and NSWindowController
  • User Defaults
  • Using Notifications
  • Using Alert Panels
  • Localization
  • Custom Views
  • Images and Mouse Events
  • Responders and Keyboard Events
  • Working with Font and NSString
  • Pasteboards and Nil-Targeted Actions
  • Categories
  • Drag-n-Drop
  • NSTimer
  • Sheets
  • Creating NSFormatters
  • Printing
  • Updating Menus
  • Working with NSTextView
  • Creating Interface Builder Palettes
  • AppleScript
  • Cocoa and OpenGL
  • Creating Frameworks
  • GNUstep
  • Using Java with Cocoa (1st ed only)
  • The End

Then follows a step-through of the relevant code for the chapter, along with a discussion about why he's doing each step, as any good programming book should do. Often at the end of chapters, there are extra notes about ideas you might want to explore on your own, and programming challenges to the reader. While the user is working through these chapters and challenges, there is a good web-based discussion by readers of the book as they work through it that is well worth referring to. (Of course, typos and corrections are also available on the book's website.) Most chapters also have a few advanced discussions for those who are itching for a bit more about the specific topic. The discussions are short, but can raise your understanding up a notch.

There are many very welcomed additions to the 2nd edition, beyond corrections and small improvements to the chapters which were present in the original run. The early coverage of Bindings, NSController was very nice to see. These were introduced in Xcode when Panther was in development, and are become common time savers for connecting GUI objects. In addition, Aaron goes into some more elaborate examples than I normally see here.

Coverage of the NSUndoManager, AppleScript integration into Cocoa apps and the whys and how-tos of creating Frameworks were all fine additions. As well, there is finally a Cocoa book which goes into working with OpenGL, which often receives insufficient coverage. Mention of GNUstep was also interesting, as Linux and Macs may see new avenues for cross-development. (GNUstep was started from the open-sourced frameworks that NeXT provided at one point in the past, providing a Cocoa-like environment on *nix machines.)

The really important part of all this, and the lesson that I really have carried away from this book, is being let in on the thinking process that goes into a good Cocoa program. The habit of making good diagrams (which I tend to Graffle) made it easy for me to move into more important structures (such as those in Design Patterns and elsewhere). Many important things are covered, from memory management to the way GUI objects interact to good basic Cocoa design. It's done well enough to allow one to extrapolate from there, which is a critical skill to gain.

A couple of critical notes: I wish there was a chapter going over error-handling, compilation errors and debugging techniques for beginners. Should I litter my code with NSLog() statements? What's the best way to use the gbd tools in ProjectBuilder? (To be fair, these have been changed since this book came out -- just suggesting for the eventual 2nd edition) Maybe include a short chapter going over the extra tools on the Developer CD that can help out. I'd also like a bit more discussion, or at least a few reminders, about some basic issues that trip-up beginners when learning both Cocoa and Objective-C for the first time (such as the pointer / struct issue).

Desiring more: I found myself wanting certain topics expanded, especially things like categories and thread safety. It might also be nice to have more "challanges" for the reader to work on, since they're so useful. Also, perhaps some more discussion about the design of objects, and groups of interacting objects, though this may be a better topic for a specific book on OO design patterns in Cocoa. And, as long as I have the podium, maybe a quick example of wrapping an external C library into an Objective-C object (such as those for MySQL or PostgreSQL). -- Maybe this is wanting to put too much advanced material in an introductory book. If I had a pile of money to spare, I'd simply commission a volume two of this work.

The bottom line: No Cocoa book is currently enough in terms of examples and discussion of certain topics. I have several, and I start glancing through them, each in turn, when I get stuck. The Cocoa framework is large, so I'd suggest buying more than one book. Make sure this is the first one you pick up. It's the best book for a beginning Cocoa programmer. You'll become at least productive enough to gain a sense of where to go from here.