Preface xviiAcknowledgments xix Chapter 1: Cocoa: What Is It? 1A Little History 1Tools 3Language 4Objects, Classes, Methods, and Messages 4Frameworks 6How to Read This Book 6Typographical Conventions 7Common Mistakes 7How to Learn 8 Chapter 2: Let's Get Started 9In Xcode 9In Interface Builder 13Back in Xcode 23Documentation 29What Have You Done? 30 Chapter 3: Objective-C 33Creating and Using Instances 33Using Existing Classes 35Creating Your Own Classes 46The Debugger 58What Have You Done? 62For the More Curious: How Does Messaging Work? 62Challenge 64 Chapter 4: Memory Management 65Turning the Garbage Collector On and Off 66Living with the Garbage Collector 68Living with Retain Counts 68What Have You Done? 77 Chapter 5: Target/Action 79Some Commonly Used Subclasses of NSControl 81Start the SpeakLine Example 85Lay Out the Nib File 86Implementing the AppController Class 88For the More Curious: Setting the Target Programmatically 90Challenge 90Debugging Hints 92 Chapter 6: Helper Objects 95Delegates 96The NSTableView and Its dataSource 99Lay Out the User Interface 102Make Connections 103Edit AppController.m 105For the More Curious: How Delegates Work 108Challenge: Make a Delegate 109Challenge: Make a Data Source 110 Chapter 7: Key-Value Coding; Key-Value Observing 111Key-Value Coding 111Bindings 113Key-Value Observing 115Making Keys Observable 116Properties and Their Attributes 118For the More Curious: Key Paths 120For the More Curious: Key-Value Observing 121 Chapter 8: NSArrayController 123Starting the RaiseMan Application 124Key-Value Coding and nil 132Add Sorting 133For the More Curious: Sorting without NSArrayController 134Challenge 1 135Challenge 2 135 Chapter 9: NSUndoManager 139NSInvocation 139How the NSUndoManager Works 140Adding Undo to RaiseMan 142Key-Value Observing 145Undo for Edits 146Begin Editing on Insert 149For the More Curious: Windows and the Undo Manager 151 Chapter 10: Archiving 153NSCoder and NSCoding 154The Document Architecture 157Saving and NSKeyedArchiver 161Loading and NSKeyedUnarchiver 162Setting the Extension and Icon for the File Type 163For the More Curious: Preventing Infinite Loops 166For the More Curious: Creating a Protocol 167For the More Curious: Document-Based Applications without Undo 167Universal Type Identifiers 168 Chapter 11: Basic Core Data 171NSManagedObjectModel 171Interface 173How Core Data Works 180 Chapter 12: Nib Files and NSWindowController 183NSPanel 183Adding a Panel to the Application 184For the More Curious: NSBundle 194Challenge 195 Chapter 13: User Defaults 197NSDictionary and NSMutableDictionary 198NSUserDefaults 200Setting the Identifier for the Application 202Creating Keys for the Names of the Defaults 202Registering Defaults 203Letting the User Edit the Defaults 203Using the Defaults 205For the More Curious: NSUserDefaultsController 207For the More Curious: Reading and Writing Defaults from the Command Line 207Challenge 208 Chapter 14: Using Notifications 209What Notifications Are 209What Notifications Are Not 210NSNotification and NSNotificationCenter 210Posting a Notification 212Registering as an Observer 213Handling the Notification When It Arrives 214The userInfo Dictionary 214For the More Curious: Delegates and Notifications 215Challenge 216 Chapter 15: Using Alert Panels 217Make the User Confirm the Deletion 218Challenge 221 Chapter 16: Localization 223Localizing a Nib File 224String Tables 226For the More Curious: ibtool 230For the More Curious: Explicit Ordering of Tokens in Format Strings 231 Chapter 17: Custom Views 233The View Hierarchy 233Getting a View to Draw Itself 235Drawing with NSBezierPath 240NSScrollView 242Creating Views Programmatically 245For the More Curious: Cells 245For the More Curious: isFlipped 247Challenge 248 Chapter 18: Images and Mouse Events 249NSResponder 249NSEvent 249Getting Mouse Events 251Using NSOpenPanel 251Composite an Image onto Your View 256The View's Coordinate System 258Autoscrolling 261For the More Curious: NSImage 261Challenge 262 Chapter 19: Keyboard Events 263NSResponder 265NSEvent 265Create a New Project with a Custom View 266For the More Curious: Rollovers 274The Fuzzy Blue Box 275 Chapter 20: Drawing Text with Attributes 277NSFont 277NSAttributedString 278Drawing Strings and Attributed Strings 280Making Letters Appear 281Getting Your View to Generate PDF Data 283For the More Curious: NSFontManager 286Challenge 1 286Challenge 2 286 Chapter 21: Pasteboards and Nil-Targeted Actions 287NSPasteboard 288Add Cut, Copy, and Paste to BigLetterView 289Nil-Targeted Actions 290For the More Curious: Which Object Sends the Action Message? 293For the More Curious: Lazy Copying 293Challenge 1 294Challenge 2 294 Chapter 22: Categories 295Add a Method to NSString 295For the More Curious: Declaring Private Methods 297For the More Curious: Declaring Informal Protocols 297 Chapter 23: Drag-and-Drop 299Make BigLetterView a Drag Source 300Make BigLetterView a Drag Destination 303For the More Curious: Operation Mask 307 Chapter 24: NSTimer 309Lay Out the Interface 311Make Connections 312Adding Code to AppController 314For the More Curious: NSRunLoop 316Challenge 316 Chapter 25: Sheets 317Adding a Sheet 318For the More Curious: contextInfo 324For the More Curious: Modal Windows 325 Chapter 26: Creating NSFormatters 327A Basic Formatter 328The delegate of the NSControl 334Checking Partial Strings 335Formatters That Return Attributed Strings 337 Chapter 27: Printing 339Dealing with Pagination 339For the More Curious: Am I Drawing to the Screen? 344Challenge 344 Chapter 28: Web Service 345AmaZone 346Lay Out the Interface 347Write Code 349Challenge: Add a WebView 353 Chapter 29: View Swapping 355Design 356Resizing the Window 362 Chapter 30: Core Data Relationships 365Edit the Model 365Create Custom NSManagedObject Classes 366Lay Out the Interface 369Events and nextResponder 372 Chapter 31: Garbage Collection 375Non-object Data Types 376Polynomials Example 377Instruments 383For the More Curious: Weak References 385Challenge: Do Bad Things 385 Chapter 32: Core Animation 387Creating CALayer 388Using CALayer and CAAnimation 390 Chapter 33: A Simple Cocoa/OpenGL Application 397Using NSOpenGLView 397Writing the Application 398 Chapter 34: NSTask 405Multithreading versus Multiprocessing 405ZIPspector 406Asynchronous Reads 410iPing 411Challenge: .tar and .tgz files 415 Chapter 35: The End 417Challenge 418 Index 419
The best-selling introduction to Cocoa, once again updated to cover the latest Mac programming technologies, and still enthusiastically recommended by experienced Mac OS X developers. "Aaron's book is the gold standard for Mac OS X programming books-beautifully written, and thoughtfully sculpted. The best book on Leopard development."-Scott Stevenson, www.theocacao.com "This is the first book I'd recommend for anyone wanting to learn Cocoa from scratch. Aaron's one of the few (perhaps only) full-time professional Cocoa instructors, and his teaching experience shows in the book."-Tim Burks, software developer and creator of the Nu programming language, www.programming.nu "If you're a UNIX or Windows developer who picked up a Mac OS X machine recently in hopes of developing new apps or porting your apps to Mac users, this book should be strongly considered as one of your essential reference and training tomes." -Kevin H. Spencer, Apple Certified Technical Coordinator If you're developing applications for Mac OS X, Cocoa (R) Programming for Mac (R) OS X, Third Edition, is the book you've been waiting to get your hands on. If you're new to the Mac environment, it's probably the book you've been told to read first. Covering the bulk of what you need to know to develop full-featured applications for OS X, written in an engaging tutorial style, and thoroughly class-tested to assure clarity and accuracy, it is an invaluable resource for any Mac programmer. Specifically, Aaron Hillegass introduces the three most commonly used Mac developer tools: Xcode, Interface Builder, and Instruments. He also covers the Objective-C language and the major design patterns of Cocoa. Aaron illustrates his explanations with exemplary code, written in the idioms of the Cocoa community, to show you how Mac programs should be written. After reading this book, you will know enough to understand and utilize Apple's online documentation for your own unique needs. And you will know enough to write your own stylish code. Updated for Mac OS X 10.4 and 10.5, this revised edition includes coverage of Xcode 3, Objective-C 2, Core Data, the garbage collector, and CoreAnimation.
Aaron Hillegas runs Big Nerd Ranch, well-known for its popular Cocoa programming classes. Previously, he was a developer at NeXT and Apple. At Next, he wrote the first course on OpenStep, the predecessor to today's Cocoa tools. At Apple, he created and taught courses in Cocoa directly for and to Apple engineers. This book is based on Aaron's Big Nerd Ranch course and is influenced by 15 years of work with OpenStep and Cocoa.