Cocoa Programming for Mac OS X - Aaron Hillegass [0]
Aaron Hillegass
Adam Preble
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • 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
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearson.com
Visit us on the Web: informit.com/aw
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'8—dc23
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 Aaron’s sons, Walden and Otto
and
For Adam’s daughter, Aimee
Contents
Preface
Acknowledgments
Chapter 1 Cocoa: What Is It?
A Little History
Tools
Language
Objects, Classes, Methods, and Messages
Frameworks
How to Read This Book
Typographical Conventions
Common Mistakes
How to Learn
Chapter 2 Let’s Get Started
In Xcode
Create a New Project
The main Function
In Interface Builder
The Utility Area
The Blank Window
Lay Out the Interface
The Dock
Create a Class
Create an Instance
Make Connections
A Look at Objective-C
Types and Constants in Objective-C
Look at the Header File
Edit the Implementation File
Build and Run
awakeFromNib
Documentation
What Have You Done?
Chronology of an Application
Chapter 3 Objective-C
Creating and Using Instances
Using Existing Classes
Sending Messages to nil
NSObject, NSArray, NSMutableArray, and NSString
“Inherits from” versus “Uses” or “Knows About”
Creating Your Own Classes
Creating the LotteryEntry Class
Changing main.m
Implementing a description Method
Writing Initializers
Initializers with Arguments
The Debugger
What Have You Done?
Meet the Static Analyzer
For the More Curious: How Does Messaging