Cocoa Programming – Part I

Tue, Sep 23

This post is the first in a series reviewing the book Cocoa Programming, by Aaron Hillegass.

Let me begin by saying, Aaron’s know the topic of Cocoa programming. As a previous employee of NeXT which merged with Apple, Aaron has extensive experience teaching developers, including many Apple Engineers, how to write applications for Mac OS X.

After a brief history lesson of how NeXT Computer (founded by Steve Jobs upon leaving Apple) evolved and Cocoa came to life, Aaron dives into what most all of us developers crave from the get go, writing an application. Aaron walks through creating a simple program that ties together Xcode (developer IDE), Interface Builder (GUI layout tool) and Objective-C. There’s enough covered here to get the big picture view of how to build applications the Apple way.

The next step (no pun intended) is a closer looker at Objective-C. Although the details are left to other books/references (as would be expected) there is a good coverage of working with classes, basic data structures/types and a smattering of other topics that provide enough information for those who are new to both Cocoa and Objective-C to follow along as the book progresses.

The next chapter is all about memory management. With the release of Mac OS X 10.5 (Leopard) Apple introduced garbage collection as option for memory management. Prior to GC, it was your responsibility as the developer to properly retain and release all objects. One caveat worth mentioning, if you are focused solely on iPhone development, you might as well skim or altogether skip learning about garbage collection as the iPhone does not offer GC support.

The book continues in this very methodical manner: presenting a concept, describing relevant methods within the framework, diagramming object relationships, inserting figures/screenshots, and more often than not, showing code examples to demonstrate concepts. What follows is the list of chapters covered in the book, an extensive list for sure:

Chapter 1. Cocoa: What Is It?
Chapter 2. Let’s Get Started
Chapter 3. Objective-C
Chapter 4. Memory Management
Chapter 5. Target/Action
Chapter 6. Helper Objects
Chapter 7. Key-Value Coding; Key-Value Observing
Chapter 8. NSArrayController
Chapter 9. NSUndoManager
Chapter 10. Archiving
Chapter 11. Basic Core Data
Chapter 12. Nib Files and NSWindowController
Chapter 13. User Defaults
Chapter 14. Using Notifications
Chapter 15. Using Alert Panels
Chapter 16. Localization
Chapter 17. Custom Views
Chapter 18. Images and Mouse Events
Chapter 19. Keyboard Events
Chapter 20. Drawing Text with Attributes
Chapter 21. Pasteboards and Nil-Targeted Actions
Chapter 22. Categories
Chapter 23. Drag-and-Drop
Chapter 24. NSTimer
Chapter 25. Sheets
Chapter 26. Creating NSFormatters
Chapter 27. Printing
Chapter 28. Web Service
Chapter 29. View Swapping
Chapter 30. Core Data Relationships
Chapter 31. Garbage Collection
Chapter 32. Core Animation
Chapter 33. A Simple Cocoa/OpenGL Application
Chapter 34. NSTask
Chapter 35. The End

In the next section of this review, I’ll provide some insight as to the code examples included in the book.


I also have to recommend this book. For me, I am a Windows .NET programmer and when I started to develop for the iPhone I really felt lost. It was not the code really, but the mindset around Cocoa and Objective-C. This book will give you just the right about of flavor and detail to point you in the right direction. He does a good job of somehow showing you the big picture by using details.

Its a must read for anyone new to Mac or iPhone programming.

by MattjDrake on Sep 23, 2008. #

In general the book is very well written but the examples sidestep the great mystery of cocoa (at least it is a mystery for this ex-windows developer):

In MVC architechture, exactly how does a view tell the model (the document) that it needs to be updated? The examples in the book either assume that all relevant data is local to the view, or they assume that your application is editing a simple array so you can use core data to update a simple table.

In fact, how can any two objects communicate? All of the objects in a cocoa app appear to be instantiated by hidden code. And unless everything it to be done with notifications you need to know which object to pass messages to. For example, how does an NSView get a message to the corresponding NSDocument? An NSView can find the NSWindow. But while the book talks about an “automatic” NSWindowController, when you ask the NSWindow for its controller, you get null. More or less a dead end, unless you subclass the NSDocument for the sole purpose of creating a global pointer to itself.

I’m sure my confusion is due to years of Windows programming brain rot and that there are simple answers to these questions – they just arent in the book. Couldnt there have been an example of how a real world application might update a document in response to user events processed by a view?

by McBane on Oct 6, 2008. #