I've been gearing up for some client work using Cocoa, and along the way I had the pleasure of being a tech reviewer for Daniel Steinberg's latest book Cocoa Programming: A Quick-Start Guide for Developers. I don't often review books here, but this book is exceptional. I think it has the potential to help folks who are interested in building native Mac (and iPhone) applications finally break into Cocoa development. The book was just released as a beta. Here's a rundown of what's inside and how it's been helpful to me.
After getting a quick lay of the land in the first chapter, Chapter 2 jumps right into building a simple web browser in Cocoa. That sounds fairly complicated for your first Cocoa app. I remember bracing for the unavoidable moment when I'd have to copy in a bunch of code I didn't understand. Thankfully, it's not that kind of book.
What I like most about this book is that it makes learning Cocoa fun and accessible. At the same time, I never felt like Daniel was waving his hands or dumbing anything down. He respected that I was a programmer, but didn't take advantage of that by making me wade through unfamiliar code to get to the good stuff. After all, a simple web browser in Cocoa is just a text field, a web view, and two buttons to navigate forward and back. You can lay out and wire up those components to act like a browser using Interface Builder without writing a single line of code. That's what the second chapter is all about: getting visual early! When I'm learning a new set of tools, I like to focus on one thing at a time. In this case, I was introduced to Interface Builder without having to tackle Objective-C, and I chalked up a quick win.
Of course, you'll need to write some code to make your web browser different from all the others. But again, you really don't want to stop and learn the entire Objective-C language just to add a new feature to the web browser. Chapter 3 is a gentle introduction to sending messages in Objective-C. If you've never seen the selector syntax, it can be a little awkward at first. The best way I've found to learn how it works is to start sending messages to existing Cocoa libraries. It turns out you can practice this in Interface Builder too. By the end of Chapter 3 my web browser and I were a little smarter.
Once you're comfortable with sending messages to existing objects, you're ready to create some Objective-C classes and objects of your own. Interface Builder can only do so much. Chapter 4 is where the rubber meets the road. Daniel walks you through creating a controller and how to make connections with Interface Builder through actions and outlets. I really like how he starts with a controller that's just a middleman, then incrementally expands its responsibility. It doesn't take a lot of Objective-C code, but it was just enough to make me feel comfortable creating classes with methods and instance variables. Daniel kept building up my confidence, one step at a time. And doing the exercises in each chapter kept me engaged.
Sending messages to a controller is one way to respond to user actions, but it's only part of the picture. In most Cocoa applications there's lot of stuff happening behind the scenes, too. The Cocoa frameworks can call back into your application via delegate methods and send notifications when certain things happen. Chapter 5 shows you how to use delegates and notifications to update the browser's title bar when a page finishes loading and update a progress bar as it's loading. Again, the process of building an application is driving what you need to learn, and when. That's exactly the way I like to new learn things.
At this point in the book I'm feeling pretty good about myself, and I'm really enjoying learning something new. I'm also surprised to find myself liking Objective-C. But I know that Daniel has been kindly delaying the topic of memory management until it's necessary, and it's been a few years since I've had to clean up after myself. The Cocoa event loop does a lot of this for you, and Objective-C 2.0 includes a garbage collector, but Cocoa applications often need to take more direct control (and iPhone apps always do). Chapter 6 steps away from the web browser to show you how to create objects with properties, and how to make them good stewards of memory. I suspect everyone has a slightly different mental model for understanding the retain-release cycle. This chapter was the first explanation of Objective-C memory management that really worked for me.
As this book is currently in beta, it's not yet complete. I'm very much looking forward to Daniel's new chapters as they come. That said, this beta book has already given me everything I need to start building my own Cocoa applications. And that's really the point. It's designed to get you up and running quickly, and help you through the first couple weeks of moving to Cocoa. To that end, I think Cocoa Programming: A Quick-Start Guide for Developers is simply brilliant!
Disclaimer: I'm reviewing this book because I like it and Daniel is a friend. I'm not employed by The Pragmatic Programmers, nor do I make any money from the sale of their fine books (except those that I write, of course). I do, however, own and operate The Pragmatic Studio where Daniel will be teaching an upcoming Cocoa Studio.