Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596804817/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
Im not sure if this is the first book youve picked up to learn Cocoa, but I think its the one that will get you started writing apps. I started teaching Cocoa and Objective-C in 2004, and I have worked with a lot of people who wanted to learn how to write software so that they could get their ideas onto the screen. But theres a problem.
There are two kinds of people who want to learn programming. Those in the first group are wired for the algorithmic mindset; theyre interested in data and the inner workings of things for their own sake. When they see a dog catch a Frisbee, they think of the calculations the dog does to catch it. Programming is a natural extension of this mindset. My guess is around five percent of the population is actually built this way.
The second (much larger) group has ideas for software that they desperately want to make real. Theyre often graphic or interaction designers. You probably have ideas about a fantastic Mac, iPhone, or iPad app that you want to create, but you dont have a million dollars to hire an engineering staff. Its very likely that you even like programming and data in addition to several other interests, but you dont see everything in terms of algorithms.
The problem is that most technical books are written by and for people in the first group. That means most of the material is being created for those who need the least help. As a result, a lot of books on programming go unread. In fact, theres this unspoken honor given to anyone who actually finishes reading one: Wow, he must be really motivated.
My problem with this is that there are a lot of people with great ideas in the second group. Many of my favorite Mac and iPhone apps today come from developers without a formal computer science background, most likely because they bring different experiences into the mix. I want to encourage more of this. Fortunately, the good folks at OReilly agree with me.
So heres the deal. I wanted to write this book because I want to help you learn how to write Mac, iPhone, and iPad apps. I want you to read it so that I get to use whatever software you end up creating.
This book is made to be accessible to new programmers, but its not watered down. Youre learning to use the same things the professionals use. My job is to make sure that each page says something useful. For each paragraph, Ive asked myself, Does this help you write your app? Anything that didnt meet that standard got cut. But I havent sold you short; if theres something you need to know to be a good Mac programmer, Ive at least told you about it. However, I havent spent time on minutiae that dont matter for Cocoa.
The content of this book is based on Cocoa tutorials I wrote between 2003 and 2009. Many of these were published at my personal site, Theocacao, and some of the longer ones were published at Cocoa Dev Central, a site I didnt originally create but have run since 2004. Ive refined the tutorials based on a one-on-one mentoring program that I ran over the same period of time. You get the benefit of all those efforts in a single condensed book.
Your job is to go write world-class Mac, iPhone, and iPad apps, and to tell everyone else how great Cocoa is. Lets get started.
Who This Book Is For
This book is for people who want to learn to make great Cocoa apps. I dont assume that you already know how to program, or anything about Objective-C or C. You do need to own an Intel-based Mac running Mac OS X 10.6 Snow Leopard, and you must know how to install software, launch apps, edit and save files, and so on. Essentially, you have to know how to use a Mac.
If you know any computer languages at all (even HTML), things will make more sense from the start. If not, youll still be able to make it through this book, but you may find some parts challenging. Even though Cocoa makes many common tasks easy, your brain has to adjust to the basic concepts of programming.
To be clear, though, this book is not exclusively for novice programmers. Depending on your experience level, you can skip a few of the chapters that are designed for beginners and jump right to the parts that are relevant to your experience.
If you already know C but havent done object-oriented programming, start with Chapter 1, which will walk you through creating a basic Cocoa application. Then move onto Chapter 4, which introduces object-oriented concepts.
If you know C and at least one object-oriented language (such as Java, Ruby, or C++), you can start with Chapter 1 for the basic orientation, and then jump ahead to Chapter 5, which introduces Objective-C.
How This Book Is Organized
The chapters in this book are organized as follows:
To build Cocoa apps, youll need to know your way around Xcode. Although well get into Xcode more deeply later, this chapter gives you a quick tour.
Before you can start programming in Objective-C (the native programming language used with Cocoa), youll need a background in programming as well as in the C language. This chapter gets you started with the basics of programming in C.
A programming language spends all its time moving things around in memory. This chapter explains how C manages memory and also explains pointers, which let you work directly with memory locations. Although you wont need all the low-level memory manipulation that C is capable of, an understanding of it will help you better understand Objective-C.
Heres where we take a detour from the C language and get into the object-oriented world. In this chapter, youll learn about classes, inheritance, objects, and more.