Objective-C FundamentalsApr 9, 2011 · 5 minute read · Comments
There seems to be a never ending number of books which provide an introduction to iPhone and iOS programming. Both the introductory books that I used when starting out with the iPhone are now long overdue an update so I have been on the lookup for something to recommend to new iOS programmers for a while.
Objective-C Fundamentals by Fairbairn, Fahrenkrug and Ruffenach (Manning Publications) takes a slightly different approach to most such books by focussing on the Objective-C language rather than Cocoa Touch. The Cocoa Touch framework is mentioned but more as a way to illustrate aspects of the language. The book is subtitled “For iOS 4 and the iPad” so the example code and framework discussions are targeted at iOS rather than Mac OS X. Having said that most of the language concepts (with the possible exception of memory management) apply equally to OS X programming.
This review is based on an early access edition so there could be some minor changes in the final released version.
Part 1 Introduction to Objective-C
The book starts with the creation of a simple app to introduce Xcode and related tools (examples use Xcode 3). Once that is out of the way it introduces the basic data types. A lot of this discussion is not really Objective-C specific since the data types discussed are really C data types. So C structures and arrays are covered for example. If you are coming to iOS programming with prior knowledge of C this will not be anything new and you can probably skim through the early chapters. However for somebody without much experience of C these earlier chapters are well done. There is a fuller discussion of C basics in the appendix for those that need it.
Things get more interesting when the concept of objects are introduced and I think the authors do a good job of covering the Objective-C way of doing things. The NSString class gets good coverage as do the NSArray and NSDictionary classes. All of which need to be well understood by an iOS programmer. Enumeration and the boxing of basic data types so that they can be stored as objects are also covered. There is some good attention to detail here as some of the more obscure parts of the language that are often overlooked get explained (for example the difference between nil, NULL and NSNull).
Part 2 Creating and Extending Classes
With the basics out of the way the book starts to dig into the core of the Objective-C language. The sections on creating classes cover declaring instance variables and methods. The key concept of getter and setter methods is introduced first before covering the use of property and synthesize declarations and the dot syntax. Subclassing is covered but so are Categories as an alternative to subclassing which is useful. Interestingly there is also a discussion of Class Clusters. I should say that advanced topics such as Class Clusters are not covered to the detail you would find in a book such as Cocoa Design Patterns but it is good to see that they at least get a mention and brief explanation.
The chapter on protocols uses the UITableView as one of several examples but as I said previously this is to illustrate the language concept more than to explain iOS table view controllers. The chapter on dynamic typing covers another of the less well understood Objective-C topics digging into messaging, selectors and runtime typing.
A key chapter in any book on Objective-C or Cocoa has to be the one on memory management. The authors explain memory management through object ownership and use that to introduce the retain/release methods (there is no discussion of garbage collection since this is an iOS book). Autorelease pools are covered as expected but so are memory zones which I confess I have never had to use with iOS. Overall this is a good explanation of a topic that most new iOS programmers struggle with. My only criticism is that you have to wait until chapter 9 for such a key topic to be covered.
Part 3 Advanced Topics
The final section of the book covers a number of advanced topics not all of which are really Objective-C language features. Having said that I think the chapter on NSError is not out of place as it covers why it is often used in Cocoa applications where other languages might instead use exception handling. The chapters on Key Value Coding (KVC), blocks and Grand Central Dispatch are all good introductions to complex topics which are becoming increasingly important for iOS developers.
The one topic that seemed slightly out of place to me was the one on Core Data. There is nothing wrong with the chapter, it is a good introduction to an important topic, I just think it would fit better in a book focussed on the Cocoa frameworks rather than Objective-C. The final chapter covers debugging with emphasis on detecting memory issues such as leaks and zombies. There is no mention of unit testing which would have been a nice addition.
Who Should Read This Book?
With so many books available all wanting to help you master iOS programming it can be difficult to recommend one over another.
I would recommend Objective-C fundamentals to people coming new to the language, perhaps without prior knowledge of C, who really want to master the finer points of the language. The only caveat is that you will probably want to start with a book that covers more of the Cocoa Touch frameworks and iOS development. This book is more suited to filling the gaps in your understanding of Objective-C rather than learning iOS. As such it is likely to be more useful to intermediate iOS developers rather than absolute beginners.
So to summarise this still may not be the first book I recommend you buy on iOS programming but it would make a good second follow up book for many people.