Cocoa Programming for Mac OS X, Fourth Edition
Aaron Hillegass
Adam Preble
Upper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web:
Library of Congress Cataloging-in-Publication Data
Hillegass, Aaron.
Cocoa programming for Mac OS X / Aaron Hillegass, Adam Preble.4th ed.
p. cm.
Includes index.
ISBN 978-0-321-77408-8 (pbk. : alk. paper)
1. Cocoa (Application development environment) 2. Operating systems
(Computers) 3. Mac OS. 4. Macintosh (Computer)Programming. I. Preble, Adam. II. Title.
QA76.76.O63H57145 2012
005.26'8dc23
2011034459
Copyright 2012 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-77408-8
ISBN-10: 0-321-77408-6
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, November 2011
For Aarons sons, Walden and Otto
and
For Adams daughter, Aimee
Contents
Preface
If you are developing applications for the Mac, or are hoping to do so, this book is just the resource you need. Does it cover everything you will ever want to know about programming for the Mac? Of course not. But it does cover probably 80% of what you need to know. You can find the remaining 20%the 20% that is unique to youin Apples online documentation.
This book, then, acts as a foundation. It covers the Objective-C language and the major design patterns of Cocoa. It will also get you started with the two most commonly used developer tools: Xcode and Instruments. After reading this book, you will be able to understand and utilize Apples online documentation.
There is a lot of code in this book. Through that code, we will introduce you to the idioms of the Cocoa community. Our hope is that by presenting exemplary code, we can help you to become more than a Cocoa developera stylish Cocoa developer.
This fourth edition includes technologies introduced in Mac OS X 10.6 and 10.7. These include Xcode 4, ARC, blocks, view-based table views, and the Mac App Store. We have also devoted one chapter to the basics of iOS development.
This book is written for programmers who already know some C programming and something about objects. If you dont know C or objects, you should first read Objective-C Programming: The Big Nerd Ranch Guide. You are not expected to have any experience with Mac programming. This hands-on book assumes that you have access to Mac OS X and the developer tools. Xcode 4.2, Apples IDE, is available for free. If you are a member of the paid Mac or iOS Developer Programs, Xcode can also be downloaded from the Apple Developer Connection Web site (http://developer.apple.com/). Enrollment in these programs enables you to submit your applications to the Mac and iOS App Stores, respectively.
We have tried to make this book as useful for you as possible, if not indispensable. That said, wed love to hear from you at if you have any suggestions for improving it.
Aaron Hillegass and Adam Preble
Acknowledgments
Creating this book required the efforts of many people. We want to thank them for their help. Their contributions have made this a better book than we could have ever written alone.
Thanks to the students who took the Cocoa programming course at the Big Nerd Ranch. They helped us work the kinks out of the exercises and explanations that appear here. Their curiosity inspired us to make the book more comprehensive, and their patience made it possible.
Thank you to all the readers of the first three editions, who made such great suggestions on our forums (http://forums.bignerdranch.com/).
Thank you to all the instructors at the Ranch, who made great additions and caught many of our most egregious errors.
A final shout out to the people at Addison-Wesley, who took our manuscript and made it into a book. They put the book on trucks and convinced bookstores to put it on the shelves. Without their help, it would still be just a stack of paper.
Chapter 1. Cocoa: What Is It?
A Little History
The story of Cocoa starts with a delightful bit of history. Once upon a time, two guys named Steve started a company called Apple Computer in their garage. The company grew rapidly, so they hired an experienced executive named John Sculley to be its CEO. After a few conflicts, John Sculley moved Steve Jobs to a position where he had no control over the company at all. Steve Jobs left to form another computer company, called NeXT Computer.
NeXT hired a small team of brilliant engineers. This small team developed a computer, an operating system, a printer, a factory, and a set of development tools. Each piece was years ahead of competing technologies, and the masses were excited and amazed. Unfortunately, the excited masses did not buy either the computer or the printer. In 1993, the factory was closed, and NeXT Computer, Inc., became NeXT Software, Inc.
The operating system and the development tools continued to sell under the name NeXTSTEP. While the average computer user had never heard of NeXTSTEP, it was very popular with several groups: scientists, investment banks, and intelligence agencies. These were people who developed new applications every week, and they found that NeXTSTEP enabled them to implement their ideas faster than any other technology.
What was this operating system? NeXT decided to use Unix as the core of NeXTSTEP. It relied on the source code for BSD Unix from the University of California at Berkeley. Why Unix? Unix crashed much less frequently than Microsoft Windows or Mac OS and came with powerful, reliable networking capabilities.
Apple has made the source code to the Unix part of Mac OS X available under the name Darwin. A community of developers continues to work to improve Darwin. You can learn more about Darwin at www.macosforge.org.