Preface

I’m not sure if this is the first book you’ve picked up to learn Cocoa, but I think it’s 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 there’s a problem.

There are two kinds of people who want to learn programming. Those in the first group are wired for the algorithmic mindset; they’re 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. They’re often graphic or interaction designers. You probably have ideas about a fantastic Mac, iPhone, or iPad app that you want to create, but you don’t have a million dollars to hire an engineering staff. It’s very likely that you even like programming and data in addition to several other interests, but you don’t 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, there’s 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 O’Reilly agree with me.

So here’s 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 it’s not watered down. You’re learning to use the same things the professionals use. My job is to make sure that each page says something useful. For each paragraph, I’ve asked myself, “Does this help you write your app?” Anything that didn’t meet that standard got cut. But I haven’t sold you short; if there’s something you need to know to be a good Mac programmer, I’ve at least told you about it. However, I haven’t spent time on minutiae that don’t 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 didn’t originally create but have run since 2004. I’ve 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. Let’s get started.

Who This Book Is For

This book is for people who want to learn to make great Cocoa apps. I don’t 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, you’ll 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 haven’t 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:

Chapter 1

To build Cocoa apps, you’ll need to know your way around Xcode. Although we’ll get into Xcode more deeply later, this chapter gives you a quick tour.

Chapter 2

Before you can start programming in Objective-C (the native programming language used with Cocoa), you’ll need a background in programming as well as in the C language. This chapter gets you started with the basics of programming in C.

Chapter 3

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 won’t need all the low-level memory manipulation that C is capable of, an understanding of it will help you better understand Objective-C.

Chapter 4

Here’s where we take a detour from the C language and get into the object-oriented world. In this chapter, you’ll learn about classes, inheritance, objects, and more.

Chapter 5

Now that you have a basic understanding of object-oriented concepts, it’s time to move on to Objective-C. This chapter explains Objective-C’s syntax for calling methods, defining classes, and creating objects.

Chapter 6

Before you can get into Cocoa, there are a few more things you need to learn about Objective-C. This chapter introduces some intermediate Objective-C concepts, including memory management, categories, selectors, and more.

Chapter 7

Although you can (and sometimes will) use standard C types in your Cocoa apps, Objective-C offers a rich set of classes for working with primitive values, such as integers, floating-point numbers, and strings. This chapter shows you how to use these value classes.

Chapter 8

Cocoa’s AppKit user interface layer allows you to create applications with rich user interfaces. This chapter prepares you to work with the built-in controls and connect them with actions you define in your code.

Chapter 9

Model-View-Controller is the mindset that guides the way you’ll put your Cocoa apps together. In this chapter, you’ll learn how to write code that coordinates your data and user interfaces.

Chapter 10

Cocoa includes a rich set of classes for displaying graphics in your apps. In this chapter, you’ll learn how to work with shapes, images, gradients, and more.

Chapter 11

Now that you’ve read through the book, you’re ready to write some apps. This short chapter gives you a few last pointers to help you on your way.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This icon signifies a tip, suggestion, or general note.

Caution

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Cocoa and Objective-C: Up and Running by Scott Stevenson. Copyright 2010 Scott Stevenson, 978-0-596-80479-4.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596804794/

A full site dedicated to the book is available from the author, and may include additional examples and announcements about Cocoa user groups and other information you may find useful as a developer. This book site is at:

http://cocoabook.com

To comment or ask technical questions about this book, send email to:

You can email the author directly at:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at:

http://www.oreilly.com

Acknowledgments

My education didn’t follow the normal path. I taught myself how to program and learned how to run a business mostly by trial and error. In the end, I think this is the only option that would have worked for me, but it was possible only because of my extremely patient and understanding family: my mom, Peggy; my dad, Alan; and my sister, Jamie. You would not be reading this now if it was not for their support.

There are so many people that have helped me in my work life, but there are a few that have had a direct impact on this book.

Michael Lopp and Angela Muller were ongoing sources of encouragement and inspiration. This book first came into being over lunch when Michael mentioned something along the lines of “everyone wants an animal on the cover of their book,” referring to the iconic O’Reilly covers. By incredible coincidence, Brian from O’Reilly emailed me about two weeks later. Had Michael not made that comment, this might not have happened.

I had world-class tech reviewers for this project: Joar Wingfors, Michael Jurewitz, Rob Rhyne, and Tim Triemstra. Joar, who I originally met through the tutorials I posted online, tirelessly reviewed an ever-changing book, provided a wealth of essential comments and suggestions, and even helped me fix some bugs in the code. He’s one of the most talented engineers I know, and I am thrilled to have been able to get his help on this.

Though he wasn’t involved in this project, I owe a lot to John Mora. He has an ability to look an impossibly large task in the face and just do it, despite the usual doubts about whether you know enough or have enough time. It is one of the most impressive traits I know of, and John has it in spades. Thankfully, I think some of that rubbed off on me over the 15 years I’ve known him.

Thanks to Kip Krueger for patiently helping me figure out the low-level details of memory and offering pointers when I was learning C. His mentoring is the reason I’m able to teach these topics to you now.

When I first started learning Cocoa, there were only two books out on the topic. I chose Aaron Hillegass’s book Cocoa Programming for Mac OS X (Addison-Wesley). Not only was it the first Mac programming book I read, it was the first technical book I actually enjoyed. His book proved to me that you could write in a direct and personal style and still be taken seriously. Having spent time with him in person, I can also add that he’s a great guy. There’s no doubt that his influence is weaved into the tutorials I’ve written.

Brian Jepson is simply a superhero among editors. I’m not sure there’s enough space to list all of the things he did to make sure this project was a success, but I certainly could not have done it without him. For any part of the book that you really like, there’s a good chance he had a hand in it. Follow him on Twitter: @bjepson.

Finally, to the thousands of people who have emailed me over the years with questions or suggestions about tutorials, I owe a lot to you as well. You helped me refine everything that ended up in this book, and by extension, helped a new generation of Cocoa developers learn how to program.

This book is dedicated to Gina and Ilya, who both taught me that life’s too short to not be spent with the people you want to be with.

Thank you.

Get Cocoa and Objective-C: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.