Cocoa & Objective-C

This blog has been pretty quiet the pass month. I haven’t posted anything since my iPad Review. I thought it was time to provide a status update with my progress learning Objective-C and Cocoa. I have spent the past month reading two books devoted to iPhone Programming and Cocoa. For this post I’ll express my thoughts on Scott Stevenson’s “Cocoa and Objective-C: Up and Running”. The second part of this post will be devoted to iPhone Programming by Joe Conway and Aaron Hillegass. That blog post should be available in the next couple of days.

Cocoa and Objective-C: Up and Running

The first book I decided to tackle was Cocoa and Objective-C: Up and Running because it was written by Scott Stevenson. A well known and respected Cocoa Developer. I have seen a couple of his CocoaHeads videos from the past and seen him host CocoaHeads WWDC08 in person. I was very impressed with his knowledge and quite fond of his cocoa tutorial site Cocoa Dev Central. So I wasn’t disappointed with his explanations of some of the most fundamental Objective-C topics. I decided to start from the beginning of the book even though I already have some experience with Objective-C and have already submitted a small iPhone utility app to the App Store. I thought it couldn’t hurt to review some basic C programming topics and Objective-C fundamentals. Chapters 2 thru 6 are very easy to digest for the experienced programmer. It was a nice primer for some advanced Objective-C concepts and sort of got me back into the mind set of a Cocoa programmer. In fact, exploring some of the key concepts of C was like going back in time 10 years ago. I remember trying to make some sense of some problem sets from my Operating Systems class in college while also trying to get a handle of the C programming language. Every homework assignment was to be written in C using vi on a Sun Ultra 10 Workstation. Professor Williams was very particular on where our homework assignments were to be completed. It has been a while since I learned pointers, dynamic memory and structs. But it was good preparation for re-learning memory management in Objective-C.

Chapter 6 was devoted to reference counting and the lifecycle of an instance variable. I loved that Stevenson shows his readers how to construct accessor methods (getters/setters) manually. For instance, he explains on why you need to autorelease the old object then retain the new one within the setter. Then he shows you how that can be done under the covers by using the @property directive. He even shows you how to use @property declarations to automatically generate instance values for “64-Bit Objective-C”. One thing I wish he didn’t do was utilize that pattern for the rest of the book. I prefer to manually declare my instance variables within the interface even though it’s consider redundant. Another concept that I thought was explained exceptionally well was Categories for Private Methods. I remember seeing some Apple sample code with an @interface directive being declared from within the implementation file and not the header. This confused the hell out of me. Now I understand that Objective-C has no formal way of creating private methods in a class. Place the category declaration in the implementation file if you care to use private methods. Chapter 7 of the book was solely devoted to Cocoa’s foundation classes. In my opinion, this chapter was dry and very difficult to read through since it read more like a reference book. It’s a very long chapter and the key gem was the explanation of Toll-Free Bridging. Again, I remember looking through sample code and seeing the exchange of NSString and CFStringRef without realizing exactly what benefit this provided a Cocoa programmer. Overall, Stevensons explanations of some of the fundamental Objective-C concepts caused me to have a quite of few ah-ha moments. It made me giddy that I was finally getting the hang of this language.

The most interesting and fun chapters of the book was 8 (Basic Controls) and 9 (Designing Applications Using MVC). In fact, I read Chapter 9 twice to make sure that I had full understanding of the concepts that were presented. Chapter 8 covered the majority of the built-in controls in AppKit. This chapter did a fine job on explaining Action/Target connections that are involved with Interface Builder and Xcode. And did a pretty good job of explaining the Responder Chain which can be a tricky concept to understand. My main gripe with this chapter was the length. It would have been better served if the chapter was broken up in pieces. My preference with programming books is to consume concepts in small parts. I try to devote myself to read one chapter per day (if manageable). Overall, I believe it took me 3 evenings to cover the concepts explained in “Basic Controls”. The same could be said about Chapter 9. There isn’t as much text to read in chapter 9. But there is a whole lot of source code to cover. Typing all the code in that chapter took me a while. Then trying to get the sample project to look, act and feel like how it was described in the book force me the read the chapter twice. There were a lot of small details that I missed when constructing the gallery application with Interface Builder. You have to be really alert to catch the small details that explain how to setup the UI with Interface Builder’s inspector. It’s very easy to skip a sentence and to miss a detail that should of been set within the attributes setting. I can’t forget to mention the way Stevenson laid out the gallery application. The reader was now able to view first-hand how an experience Cocoa developer compose their project structure. For instance, Stevenson expressed that simple cocoa projects can be constructed using the default application delegate class. All of your controls can be dropped in that one delegate class if you are writing a small app. But larger and complicated applications may be better served with separate window controllers that may or may not have their own XIB files. The same emphasis is also placed on View controllers. Creating building blocks through the utilization of sectioned view controllers and window controllers provide code portability, and ease of use in regards to code maintenance.

Overall, the book was fun to read with the lone exception of Chapter 7. But you really can’t get around the dry explanation of all the Foundation classes. There really is one way to describe an NSArray.