Beginners Guide: Programming Cocoa for OS X

XcodeA few months back a friend asked me if I wanted to help work on a small application for OS X. He figured because I had a long history of Windows based programming I would have no trouble making the transition to writing apps for Mac. Well he was wrong for obvious reasons and I also found (unlike coding Windows apps) that the journey to bring my skills up to par for Mac, was a greatly rewarding, yet somewhat convoluted experience.

Back then I put the question to Mudi, one of the devs behind ForkLift, and he came through with some really great suggestions for me (thanks dude!). What follows are some beginner’s recommendations and a brief outline for those interested in learning to build wicked cool programs for Mac’s OS X.

First of all some background. Wikipedia says it better than I ever could:

“Cocoa is Apple Inc’s native object-oriented application programming environment for the Mac OS X operating system. It is one of five major APIs available for Mac OS X; the others are Carbon, Toolbox (for the obsolete Classic environment), POSIX (for the BSD environment), and Java. (Environments such as Perl and Ruby are considered minor environments because they are not generally used for full-fledged application programming.)

Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder, using the Objective-C language. However, the Cocoa programming environment can be accessed using other tools, such as Python, Perl and Ruby, with the aid of bridging mechanisms such as PyObjC, CamelBones and RubyCocoa, respectively. It is also possible to write Objective-C Cocoa programs in a simple text editor and build it manually with GCC or GNUstep’s makefile scripts.

For end-users, Cocoa applications are considered to be those written using the Cocoa programming environment. Such applications usually have a distinctive feel, since the Cocoa programming environment automates many aspects of an application to comply with Apple’s Human Interface Guidelines.”

So if you are going to be learning to write next generation applications for OS X you will need the following:

  • A strong background with Objective C
  • A fluent understanding of Cocoa and the other APIs used to interact with OS X.
  • Xcode (currently 2.4.1) – This is Apple’s app development environment, where you will be doing pretty much all of your work.
  • Interface Builder (bundled with Xcode) – Can you guess what this does?
  • A membership (free or paid) to Apple’s Developer Connection site.
  • Some great textbook style books to get you off and running (my favorites reviewed below).
  • A ridiculous amount of caffeine fueled beverages and an understanding spouse.

Objective C Books

Programming in Objective-C
by Stephen Kochan | $23.00

Written like a text book Kochan’s Programming in Objective-C is one of the best programming books I have ever read, and the book has a good reputation. I hadn’t done any work in C/C++ since the early 1990′s and I was honestly up to speed with Object Orientated C in no time. The book is positioned at beginners, so if you have zero experience in C or programming in general you should still be fine with this one, but you may need to re-read several chapters. Although he starts off fast with some fundamentals the author wastes little time with fluff and each chapter is really packed with information. Aside from a couple of errors (see below) the example code is also really well put together and clear in the principles it is trying to convey.

As this was recommended by virtually everyone as the only book to buy for learning Objective C it was the first one I picked up on the subject. I found that I came away with enough experience and confidence to push forward into Cocoa and get started building real Mac apps.

One problem I had with this book is that there were a couple errors in the exercises that had me a bit stumped. Luckily I found the errata on Stephen’s website. Another thing you should know is that this book does not go over Cocoa at all but it is somewhat implied that you will be wanting to build apps on OS X. So for the most part the book is not platform dependent in any way.

The bottom line is, you will not be much of a Mac developer without a strong background in Objective C, and this is the reference that will help you get there. Highly recommended.

Objective-C Pocket Reference
by Andrew M. Duncan | $27.63

I have always been a sucker for O’Reilly’s pocket quick reference guides. I found that having it available on my desk really helped me read Stephen’s Programming in Objective-C I recommend above. They are perfect companions. So keep in mind that this is a 128 page reference book covering stuff like syntax, objects, memory and exception handling, and not a text book. Having said that there were a few things I just didn’t grasp until they were outlined for me in this book. This guide really became an important desk reference once I started building.

I have no gripes with this book – clear, concise, inexpensive and easy to read.

Cocoa Books

Cocoa Programming for Mac OS X (2nd Edition)
by Aaron Hillegass | $26.93

This book is very well known in Mac development circles and that’s why I mention it first. It is likely the book that most developers read first once they have a foundation built in Objective C programming. The book serves as a very good introduction to the Cocoa framework and XCode, and provides some really well written examples. If you are just starting out I would really recommend this book as Aaron is honestly a very talented teacher. I went through the exercises in this book coming away knowing that I had really mastered the subjects covered.

However, don’t expect him to go too deep in to each concept, as he tends to gloss over a few things I wish were expanded on a bit. Also some people might say that the book is a bit dated and even though it is revised, there are a couple of areas it lacks. The book was written before Xcode 2.0 and a couple of the examples demonstrate that, but in all honesty, it’s not a big problem to overcome. I also wish Aaron had provided source examples somewhere online, but I guess forcing me to type the exercises out by hand makes them stick a bit better too.

All in all highly recommended for programmer’s switching over to Mac and other beginners.

Cocoa Programming
by Scott Anguish, Erik Buck, Donald Yacktman | $99.00

Cocoa Programming is a really large SAMS book that weighs in at nearly 5 lbs. Thankfully all of the bulk is not wasted on preaching or fooling around as this book has long been considered one of the best Cocoa books you can buy. People have told me that this book has really cleaned up any final issues or misconceptions they had using Cocoa, and really added to their toolsets.

I constantly found that the authors went above and beyond explaining the classes and protocols of Cocoa, and many of the examples I find actually using in my applications. You really get the sense that the guys writing the material are crack programmers with years of experience. The most interesting areas of the book for me were the sections on Client/Server networking, and some of the Appkit stuff (which they obviously cover in-depth).

The problem with this book is that it was written in 2002. So this is way back in the OS X 10.1-10.2 days, and some of what is covered is completely deprecated. This along with the hefty price were almost enough for me to not even consider buying it. The thing that redeems this book is that it covers valid subjects with entire chapters that newer books do not even mention. The truth is when it comes to actually sitting down and doing the work I find myself constantly coming back to this one.

Additional Resources

Well I hope this helps would be OS X programmers to get started. It really seemed daunting at first but you will be in the thick of things in no time. Some final web based resources I found invaluable:

  • Again – Apple’s Developer Connection website, read this getting started guide. Check back often.
  • The developer related areas of the Apple mailing lists were often useful.
  • Andy Matuschak has a written an excellent friendly guide to getting started with Cocoa.
  • Macdevcenter.com – This is the O’Reilly networks contribution to developing on Mac. An essential resource with regular updates.
  • CocoaDev.com – A Cocoa resource site with an old school styles bulletin board.
  • Cocoadevcentral.com – One of the best resources out there. Excellent tutorials and articles with reader comments in a blog styled layout. Both internal and external sources.
  • The Mac programming forum on MacRumors has some great discussions with lots of active users.
  • iDevApps is another popular and active forum for Mac developers.
  • #macdev on Freenode, one of the biggest IRC channels for Mac development.
  • There is even a podcast for Cocoa developers although it is becoming less and less updated.
  • Update: Alex Clarke has pointed out his excellent Open Sourced project called BecomeAnXcoder. The PDF based guide is aimed at true beginners, comes in five languages, and is really very well written (I just printed it off). Thank’s Alex, not sure how I never came upon this.

What resources help you sharpen your OS X programming skills?