Cover image for Cocoa programming for Mac OS X
Cocoa programming for Mac OS X
Hillegass, Aaron.
Personal Author:
Publication Information:
Boston : Addison-Wesley, [2002]

Physical Description:
xix, 383 pages : illustrations ; 24 cm
General Note:
Includes index.
Title Subject:
Format :


Call Number
Material Type
Home Location
Item Holds
QA76.76.O63 H57145 2002 Adult Non-Fiction Central Closed Stacks

On Order



Cocoa is one of the principal object-oriented software development environments for Mac OS X. This volume is a serious developer's guide to Cocoa. It offers start-to-finish coverage from the creator of an independent Cocoa programming course. Aaron Hillegass shows how to master the design patterns that give Cocoa its power and elegance. The entire Cocoa toolset is covered - with guidance on mastering Objective C, the preferred language for Cocoa development.

Author Notes

Aaron Hillegass, who worked at NeXT and Apple, now teaches popular Cocoa programming classes at Big Nerd Ranch. At NeXT, he wrote the first course on OpenStep, the predecessor to today's Cocoa tools.This book is based on the Big Nerd Ranch course and is influenced by more than a decade of work with OpenStep and Cocoa.



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 it doesn't. But it does cover probably 80% of what you need to know. You can find the remaining 20%, the 20% that is unique to you, in Apple's 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 three most commonly used developer tools: Xcode, Interface Builder, and Instruments. After reading this book, you will be able to understand and utilize Apple's online documentation. There is a lot of code in this book. Through that code, I will introduce you to the idioms of the Cocoa community. My hope is that by presenting exemplary code, I can help you to become not just a Cocoa developer, but a stylish Cocoa developer. This third edition includes technologies introduced in Mac OS X 10.4 and 10.5. These include Xcode 3, Objective-C 2, Core Data, the garbage collector, and CoreAnimation. This book is written for programmers who already know some C programming and something about objects. You are not expected to have any experience with Mac programming. It's a hands-on book and assumes that you have access to Mac OS X and the developer tools. The developer tools are free. If you bought a shrink-wrapped copy of Mac OS X, the installer for the developer tools was on the DVD. The tools can also be downloaded from the Apple Developer Connection Web site ( ). I have tried to make this book as useful for you as possible, if not indispensable. That said, I'd love to hear from you if you have any suggestions for improving it. Aaron Hillegass Excerpted from Cocoa Programming for Mac OS X by Aaron Hillegass All rights reserved by the original copyright owners. Excerpts are provided for display purposes only and may not be reproduced, reprinted or distributed without the written permission of the publisher.

Table of Contents

Prefacep. xvii
Acknowledgmentsp. xix
Chapter 1 Cocoa: What Is It?p. 1
A Little Historyp. 1
Toolsp. 3
Objects, Classes, Methods, and Messagesp. 4
Frameworksp. 5
How to Read This Bookp. 6
Typographical Conventionsp. 7
How to Learnp. 7
Chapter 2 Let's Get Startedp. 9
In Project Builderp. 9
In Interface Builderp. 13
Back in Project Builderp. 24
Documentationp. 30
What Have You Done?p. 30
Chapter 3 Objective-Cp. 33
Creating and Using Instancesp. 33
Using Existing Classesp. 35
Creating Your Own Classesp. 49
The Debuggerp. 67
What Have You Done?p. 70
For the More Curious: How Does Messaging Work?p. 71
Challengep. 72
Chapter 4 Controlsp. 73
Some Commonly Used Subclasses of NSControlp. 75
Start the RaiseMan Examplep. 79
The Interface File for MyDocumentp. 81
Lay Out the Interfacep. 83
Implementing the Person Classp. 94
Implementing the MyDocument Classp. 96
What Have You Done?p. 100
For the More Curious: Setting the Target Programmaticallyp. 101
Challengep. 101
Chapter 5 Helper Objectsp. 103
NSTableView and Its Data Sourcep. 104
MyDocument Interface Filep. 105
Lay Out the User Interfacep. 106
Make Connectionsp. 109
Edit 111
Key-value Codingp. 113
Delegatesp. 115
For the More Curious: How Delegates Workp. 118
Challenge 1p. 119
Challenge 2p. 119
Chapter 6 Archivingp. 121
NSCoder and NSCodingp. 122
The Document Architecturep. 125
Saving and NSArchiverp. 128
Loading and NSUnarchiverp. 129
Informing the Document That It Has Been Editedp. 130
Setting the Extension and Icon for the File Typep. 131
For the More Curious: Preventing Infinite Loopsp. 134
For the More Curious: Versioningp. 135
For the More Curious: Creating a Protocolp. 136
Chapter 7 Nib Files and NSWindowControllerp. 139
NSPanelp. 139
Adding a Panel to the Applicationp. 140
For the More Curious: NSBundlep. 152
Challenge 1p. 154
Challenge 2p. 154
Chapter 8 User Defaultsp. 155
NSDictionary and NSMutableDictionaryp. 155
NSUserDefaultsp. 158
Creating Keys for the Names of the Defaultsp. 160
Registering Defaultsp. 161
Let the User Edit the Defaultsp. 161
Using the Defaultsp. 163
Saving the Frame of a Windowp. 165
For the More Curious: Reading and Writing Defaults from the Command Linep. 165
Challengep. 166
Chapter 9 Using Notificationsp. 167
What Notifications Arep. 167
What Notifications Are Notp. 168
NSNotificationp. 168
NSNotificationCenterp. 168
Posting a Notificationp. 171
Registering As an Observerp. 171
Unregistering the Observerp. 172
Handling the Notification When It Arrivesp. 172
For the More Curious: Delegates and Notificationsp. 173
For the More Curious: The userInfo Dictionaryp. 173
Challengep. 174
Chapter 10 Using Alert Panelsp. 175
Make the User Confirm the Deletep. 176
Challengep. 178
Chapter 11 Localizationp. 179
Localizing a Nib Filep. 180
String Tablesp. 182
For the More Curious: nibtoolp. 185
Challengep. 187
Chapter 12 Custom Viewsp. 189
The View Hierarchyp. 189
Get a View to Draw Itselfp. 191
Drawing with NSBezierPathp. 197
NSScrollViewp. 199
For the More Curious: Cellsp. 201
Challengep. 204
Chapter 13 Images and Mouse Eventsp. 205
NSResponderp. 205
NSEventp. 205
Getting Mouse Eventsp. 207
Using NSOpenPanelp. 207
Composite an Image Onto Your Viewp. 215
The View's Coordinate Systemp. 217
Autoscrollingp. 220
For the More Curious: NSImagep. 220
Challengep. 221
Chapter 14 Responders and Keyboard Eventsp. 223
NSResponderp. 224
NSEventp. 226
Create a New Project with a Custom Viewp. 226
Chapter 15 Working with Fonts and NSStringp. 237
NSFontp. 237
NSAttributedStringp. 238
Drawing Strings and Attributed Stringsp. 240
Making Letters Appearp. 241
Getting Your View to Generate PDF Datap. 243
For the More Curious: NSFontManagerp. 245
Challenge 1p. 246
Challenge 2p. 246
Chapter 16 Pasteboards and Nil-Targeted Actionsp. 247
NSPasteboardp. 248
Add Cut, Copy, and Paste to BigLetterViewp. 249
Nil-Targeted Actionsp. 251
For the More Curious: Which Object Really Sends the Action Message?p. 254
For the More Curious: Lazy Copyingp. 254
Challengep. 255
Chapter 17 Categoriesp. 257
Add a Method to NSStringp. 257
Chapter 18 Drag-and-Dropp. 261
Make BigLetterView a Drag Sourcep. 261
Make BigLetterView a Drag Destinationp. 264
Challengep. 268
Chapter 19 NSTimerp. 269
Lay Out the Interfacep. 269
Make Connectionsp. 271
Adding Code to AppControllerp. 273
Challengep. 276
Chapter 20 Sheetsp. 279
Adding a Sheetp. 280
For the More Curious: contextInfop. 286
For the More Curious: NSDrawerp. 287
Challengep. 288
Chapter 21 Creating NSFormattersp. 291
A Basic Formatterp. 292
The Delegate of the NSControlp. 299
Checking Partial Stringsp. 300
The Control's Delegate and Partial Stringsp. 301
Formatters That Return Attributed Stringsp. 302
Challengep. 302
Chapter 22 Printingp. 305
Adding Printing to TypingTutorp. 305
Dealing with Paginationp. 307
Challengep. 313
Chapter 23 Updating Menusp. 315
Making a BigLetterView Uncopyablep. 316
Chapter 24 Working with NSTextViewp. 321
NSTextViewp. 322
Messages the Delegate Will Be Sentp. 323
Build the Editor with Which This Book Was Writtenp. 324
For the More Curious: The Field Editorp. 337
Chapter 25 Using Java with Cocoap. 341
Don't Use Java to Write Cocoa Applicationsp. 341
If You Must Use Java to Write Cocoa Applicationsp. 342
Chapter 26 Creating Interface Builder Palettesp. 349
Add NSCoding Methods to BigLetterViewp. 350
Create a Palette Projectp. 352
Edit the Nib File for Your Palettep. 354
palette.tablep. 354
Build and Testp. 356
finishInstantiatep. 357
Adding an Inspectorp. 358
For the More Curious: How to Palettize Objects That Are Not Viewsp. 365
Challengep. 367
Chapter 27 The Endp. 369
Indexp. 371