about this book

Objective-C Fundamentals is an introductory book, intended to complement other books focused on iPhone and iPad application development such as iOS 4 in Action. While many books have been written on how to develop iOS applications, most focus on the individual APIs and frameworks provided by the device, rather than the unique language, Objective-C, which is a cornerstone of Apple’s development platform. To truly master the platform, you must have a strong grip on the language, and that is what this book intends to provide. Objective-C Fundamentals is a book that focuses on learning Objective-C in the context of iOS application development. No time is spent discussing aspects or elements of the language that are not relevant to iOS. All examples are fully usable on your own iOS-powered device. We encourage you to read this book straight through, from chapter 1 to chapter 14. This process will introduce the platform, discuss how to program for the iPhone and iPad, and walk you through the entire process step by step.

The audience

We’ve done our best to make this book accessible to everyone who is interested in creating successful iOS applications using the native Objective-C–based development tools.

If you want to learn about iOS programming, you should have some experience with programming in general. It’d be best if you’ve worked with C or at least one object-oriented language before, but that’s not a necessity. If you haven’t, you may find the introduction to the C programming language in appendix B helpful, and you should expect to do some research on your own to bolster your general programming skills. There’s no need to be familiar with Objective-C, Cocoa, or Apple programming in general. We’ll give you everything you need to become familiar with Apple’s unique programming style. You’ll probably have a leg-up if you understand object-oriented concepts; but it’s not necessary (and again, you’ll find an introduction in chapter 3).

Roadmap

Chapter 1 introduces the tools surrounding Objective-C and iOS application development, and covers the creation of a basic game, ready to run on your device.

Chapter 2 kicks things off by highlighting how data is stored and represented within an Objective-C–based application.

Chapter 3 looks at how Objective-C takes small quantities of data and packages them with logic to form reusable components called classes.

Chapter 4 shifts the focus by taking a look at some of the classes, provided out of the box by Cocoa Touch, that can be used to store multiple pieces of related data.

Chapter 5 covers how to create your own custom classes and objects. Learning how to create your own classes is an important building block to becoming a productive developer.

Chapter 6 takes a look at how you can build on top of the foundations provided by an existing class to create a more specialized or customized version of a class without needing to rewrite all of its functionality from scratch.

Chapter 7 discusses how classes can be defined to provide specific functionality, without resorting to requiring all classes to inherit from a common base class. This concept is provided with a language construct called a protocol.

Chapter 8 looks deeply at some of the aspects of Objective-C that make it unique. The important distinction between message sending and method invocation is discussed and some powerful programming techniques are demonstrated.

Chapter 9 covers how to keep track of memory allocation within an Objective-C application. Since no automatic garbage collector is available, simple rules are discussed which will allow you to expertly craft applications without introducing memory leaks.

Chapter 10 looks at NSError and at some real-life use cases for exceptions, which tools will help you deal with errors gracefully.

Chapter 11 covers Key Value Coding (KVC) and NSPredicate-based queries, which are a surprisingly flexible way to filter, search and sort data within Cocoa Touch–based applications.

Chapter 12 gets you started with Core Data and teaches you everything you’ll need to know to leverage Core Data for all of your most common data persistence needs.

Chapter 13 introduces a language construct called a block and demonstrates this by showing how Grand Central Dispatch (GCD) can be used to simplify multithreaded programming, since it takes care of all the complicated heavy lifting for us.

No application is perfect first time around, so chapter 14 rounds out the book with a discussion on debugging techniques that can help resolve unwanted logic errors and memory leaks quickly and efficiently.

The appendixes contain additional information that didn’t fit with the flow of the main text. Appendix A outlines how to enroll in the iOS Developer Program and set up your physical iPhone or iPad device in order to run your own applications on them. Appendix B provides a basic overview of the C programming language that Objective-C is a descendant of. This will be ideal for developers with little experience of a C-based language and those that have previously only developed in languages such as Ruby, Python, or Java. Appendix C outlines some of the alternatives you can use to develop iOS applications, and compares their advantages and disadvantages to Objective-C.

Writing this book was truly a collaborative effort. Chris wrote chapters 1 through 5, 8, 9, 11, 14, and appendixes B and C. Johannes contributed chapters 10, 12, and 13, and appendix A; and Collin was responsible for chapters 6 and 7.

Code conventions and downloads

Code examples appear throughout this book. Longer listings appear under clear listing headings, and shorter listings appear between lines of text. All code is set in a monospace font like this to differentiate it from the regular font. Class names have also been set in code font; if you want to type it into your computer, you’ll be able to clearly make it out.

With the exception of a few cases of abstract code examples, all code snippets began life as working programs. You can download the complete set of programs from www.manning.com/Objective-CFundamentals. You’ll find two ZIP files there, one for each of the SDK programs. We encourage you to try the programs as you read; they include additional code that doesn’t appear in the book and provide more context. In addition, we feel that seeing a program work can elucidate the code required to create it.

The code snippets in this book include extensive explanations. We often include short annotations beside the code; and sometimes numbered cueballs beside lines of code link the subsequent discussion to the code lines.

Software requirements

An Intel-based Macintosh running OS X 10.6 or higher is required to develop iOS applications. You also need to download the Xcode IDE and iOS SDK. Xcode is available for purchase in the Mac App Store and the iOS SDK is freely downloadable.

However, the best approach to obtaining Xcode and developing iOS applications is to pay a yearly subscription fee for the iOS Developer Program (http://developer.apple.com/programs/ios/). This will provide free access to Xcode and iOS SDK downloads as well as enable testing and deployment of applications on real iPhone and iPad devices, and the iTunes App Store.