You are previewing COCOA PROGRAMMING FOR MAC OS X SECOND EDITION.
O'Reilly logo
COCOA PROGRAMMING FOR MAC OS X SECOND EDITION

Book Description

A new edition of this title is available, ISBN-10: 0321503619 ISBN-13: 9780321503619

The highly acclaimed introduction to Cocoa–recommended most by experienced Mac OS X developers now updated and expanded.

Here's what critics said about the first edition:

"Reading this book is the absolute best way to learn how to harness the power of this amazing technology."
Andrew Stone, President, Stone Design, www.stone.com

"Make sure this is the first one you pick up. It's the best book for a beginning Cocoa programmer."
–From the review on HyperJeff.net&

"I love this book. The descriptions are clear, the examples logical. Everything a programmer needs to get up to speed on Cocoa."
Dave Mark, Editor, MacTech Magazine

To help programmers develop applications for Mac OS X, Apple is now giving away XCode, Interface Builder, and the Cocoa frameworks–the tools used to create Safari, GarageBand, Mail, and the iApps. Cocoa® Programming for Mac® OS X, Second Edition, will give you a complete understanding of how to use these tremendously powerful tools and frameworks to write full-featured applications for the Mac.

Guiding programmers through the key features of Cocoa, this book emphasizes design patterns that enable you to predict the behavior of classes you have never used before. Written in a tutorial format, it takes you step-by-step through the creation of six applications and an Interface Builder palette. Each project introduces several new ideas, and as each concept or technique is discussed, the author, drawing on his own extensive experience, shows you the right way to use it.

Updated for Xcode and Mac OS X 10.3, new chapters in this second edition include coverage of OpenGL, AppleScriptability, the undo manager, creating frameworks, and a brief introduction to using GNUstep on Linux.



Table of Contents

  1. Copyright
    1. Dedication
  2. Preface to the Second Edition
    1. Preface to the First Edition
  3. Acknowledgments
  4. 1. Cocoa: What Is It?
    1. A Little History
    2. Tools
    3. Language
    4. Objects, Classes, Methods, and Messages
    5. Frameworks
    6. How to Read This Book
    7. Typographical Conventions
    8. Common Mistakes
    9. How to Learn
  5. 2. Let's Get Started
    1. In Xcode
      1. Create a New Project
      2. The main Function
    2. In Interface Builder
      1. The Standard Palettes
      2. The Blank Window
      3. Lay Out the Interface
      4. The Doc Window
      5. Create a Class
      6. Create an Instance
      7. Make Connections
    3. Back in Xcode
      1. Types and Constants in Objective-C
      2. Look at the Header File
      3. Edit the Implementation File
      4. Build and Run
      5. awakeFromNib
    4. Documentation
    5. What Have You Done?
  6. 3. Objective-C
    1. Creating and Using Instances
    2. Using Existing Classes
      1. Memory Management: Retain Count, Releasing, and Retaining
      2. Sending Messages to nil
      3. NSObject, NSArray, NSMutableArray, and NSString
        1. NSObject
        2. NSArray
        3. NSMutableArray
        4. NSString
      4. “Inherits from” Versus “Uses” or “Knows About”
    3. Creating Your Own Classes
      1. Creating the LotteryEntry Class
        1. LotteryEntry.h
        2. LotteryEntry.m
        3. dealloc
      2. Changing main.m
      3. Implementing a description Method
        1. Creating Autoreleased Objects
          1. Temporary Objects
          2. Accessor Methods
        2. NSCalendarDate
      4. Writing Initializers
      5. Initializers with Arguments
    4. The Debugger
    5. What Have You Done?
    6. For the More Curious: How Does Messaging Work?
    7. Challenge
  7. 4. Controls
    1. Some Commonly Used Subclasses of NSControl
      1. NSButton
      2. NSSlider
      3. NSTextField
    2. Start the SpeakLine Example
    3. Lay Out the Nib File
      1. Making Connections in Interface Builder
        1. NSWindow's initialFirstResponder Outlet
    4. Implementing the AppController Class
      1. Extending an Existing User Interface
    5. For the More Curious: Setting the Target Programmatically
    6. Challenge
  8. 5. Helper Objects
    1. Delegates
    2. The NSTableView and Its dataSource
      1. AppController Interface File
    3. Lay Out the User Interface
    4. Make Connections
    5. Edit AppController.m
      1. Common Errors in Implementing a Delegate
      2. Many Objects Have Delegates
      3. Retain Cycles
    6. For the More Curious: How Delegates Work
    7. Challenge
  9. 6. Bindings and NSController
    1. Starting the RaiseMan Application
      1. Key-Value Coding
      2. In Interface Builder
    2. Key-Value Coding and nil
    3. Add Sorting
    4. For the More Curious: Sorting Without NSArrayController
    5. For the More Curious: Key Paths
    6. Challenge 1
    7. Challenge 2
  10. 7. NSUndoManager
    1. NSInvocation
    2. How the NSUndoManager Works
    3. Adding Undo to RaiseMan
    4. Key-Value Observing
    5. Undo for Edits
    6. For the More Curious: Windows and the Undo Manager
  11. 8. Archiving
    1. NSCoder and NSCoding
      1. Encoding
      2. Decoding
    2. The Document Architecture
      1. Info.plist and NSDocumentController
      2. NSDocument
        1. Saving
        2. Loading
      3. NSWindowController
    3. Saving and NSKeyedArchiver
    4. Loading and NSKeyedUnarchiver
    5. Setting the Extension and Icon for the File Type
    6. For the More Curious: Preventing Infinite Loops
    7. For the More Curious: Versioning
    8. For the More Curious: Creating a Protocol
    9. For the More Curious: Document-Based Applications Without Undo
  12. 9. Nib Files and NSWindowController
    1. NSPanel
    2. Adding a Panel to the Application
      1. MainMenu.nib
      2. Setting Up the Menu Item
      3. AppController.m
      4. Preferences.nib
        1. File's Owner
        2. Lay Out the User Interface
      5. PreferenceController.m
    3. For the More Curious: NSBundle
    4. Challenge 1
    5. Challenge 2
  13. 10. User Defaults
    1. NSDictionary and NSMutableDictionary
      1. NSDictionary
      2. NSMutableDictionary
    2. NSUserDefaults
      1. Precedence of Different Types of Defaults
    3. Setting the Identifier for the Application
    4. Creating Keys for the Names of the Defaults
    5. Registering Defaults
    6. Let the User Edit the Defaults
    7. Using the Defaults
      1. Suppressing the Creation of Untitled Documents
      2. Setting the Background Color on the Table View
    8. For the More Curious: Reading and Writing Defaults from the Command Line
    9. Challenge
  14. 11. Using Notifications
    1. What Notifications Are
    2. What Notifications Are Not
    3. NSNotification
    4. NSNotificationCenter
    5. Posting a Notification
    6. Registering as an Observer
    7. Unregistering the Observer
    8. Handling the Notification When It Arrives
    9. For the More Curious: Delegates and Notifications
    10. For the More Curious: The userInfo Dictionary
    11. Challenge 1
    12. Challenge 2
  15. 12. Using Alert Panels
    1. Make the User Confirm the Deletion
    2. Challenge
  16. 13. Localization
    1. Localizing a Nib File
    2. String Tables
      1. Creating String Tables
      2. Using the String Table
    3. For the More Curious: nibtool
    4. For the More Curious: Explicit Ordering of Tokens in Format Strings
  17. 14. Custom Views
    1. The View Hierarchy
    2. Get a View to Draw Itself
      1. Create an Instance of a View Subclass
      2. Size Info
      3. drawRect:
    3. Drawing with NSBezierPath
    4. NSScrollView
    5. For the More Curious: Cells
    6. For the More Curious: isFlipped
    7. Challenge
  18. 15. Images and Mouse Events
    1. NSResponder
    2. NSEvent
    3. Getting Mouse Events
    4. Using NSOpenPanel
      1. Change the Nib File
      2. awakeFromNib Versus init
      3. Edit the Code
    5. Composite an Image onto Your View
    6. The View's Coordinate System
    7. Autoscrolling
    8. For the More Curious: NSImage
    9. Challenge
  19. 16. Responders and Keyboard Events
    1. NSResponder
    2. NSEvent
    3. Create a New Project with a Custom View
      1. Edit the Nib File
        1. Lay Out the Interface
        2. Size Info
        3. Make Connections
      2. Write the Code
        1. In BigLetterView.h
        2. In BigLetterView.m
    4. For the More Curious: Rollovers
  20. 17. Working with Fonts and NSAttributedString
    1. NSFont
      1. Commonly Used Methods in NSFont
    2. NSAttributedString
    3. Drawing Strings and Attributed Strings
    4. Making Letters Appear
    5. Getting Your View to Generate PDF Data
    6. For the More Curious: NSFontManager
    7. Challenge 1
    8. Challenge 2
  21. 18. Pasteboards and Nil-Targeted Actions
    1. NSPasteboard
    2. Add Cut, Copy, and Paste to BigLetterView
    3. Nil-Targeted Actions
      1. Looking at the Nib File
    4. For the More Curious: Which Object Really Sends the Action Message?
    5. For the More Curious: Lazy Copying
    6. Challenge
  22. 19. Categories
    1. Add a Method to NSString
    2. For the More Curious: Declaring Private Methods
  23. 20. Drag-and-Drop
    1. Make BigLetterView Be a Drag Source
      1. After the Drop
    2. Make BigLetterView Be a Drag Destination
      1. registerForDraggedTypes
      2. Add Highlighting
      3. Implement the Dragging Destination Methods
      4. Testing
  24. 21. NSTimer
    1. Lay Out the Interface
    2. Make Connections
    3. Adding Code to AppController
    4. For the More Curious: NSRunLoop
    5. Challenge
  25. 22. Sheets
    1. Adding a Sheet
      1. Add Outlets and Actions
      2. Lay Out the Interface
      3. Add Code
    2. For the More Curious: contextInfo
    3. For the More Curious: Modal Windows
    4. For the More Curious: Alert Sheets
    5. For the More Curious: NSDrawer
    6. Challenge
  26. 23. Creating NSFormatters
    1. A Basic Formatter
      1. Edit the Interface File for the AppController Class
      2. Edit the Nib File
      3. NSColorList
      4. Searching Strings for Substrings
      5. Create a Subclass of NSFormatter
    2. The Delegate of the NSControl
    3. Checking Partial Strings
    4. Formatters That Return Attributed Strings
    5. Challenge
  27. 24. Printing
    1. Adding Printing to TypingTutor
    2. Dealing with Pagination
    3. For the More Curious: Am I Drawing to the Screen?
    4. Using ObjectAlloc
    5. Challenge
  28. 25. Updating Menus
    1. Making a BigLetterView Uncopyable
    2. For the More Curious: Menu Delegates
  29. 26. Working with NSTextView
    1. NSTextView
    2. Messages the Delegate Will Be Sent
    3. Build the Editor with Which This Book Was Written
      1. Read, Write, and Edit Text Files
      2. Add a Context-Sensitive Menu
        1. Adding the Menu
        2. Creating the NSMenu
      3. Replace the Selection
    4. For the More Curious: The Field Editor
    5. Challenge
  30. 27. Creating Interface Builder Palettes
    1. Add NSCoding Methods to BigLetterView
    2. Create a Palette Project
    3. Edit the Nib File for Your Palette
    4. palette.table
    5. Build and Test
    6. finishInstantiate
    7. Adding an Inspector
      1. Create a Nib File for Your Inspector
      2. Code for an Inspector
    8. Adding Bindings to a Custom View
    9. For the More Curious: How to Palettize Objects That Are Not Views
    10. Challenge
  31. 28. AppleScript
    1. Making an Application AppleScript-able
    2. Create the Plists
    3. Handling the Apple Events
  32. 29. Cocoa and OpenGL
    1. A Simple Cocoa/OpenGL Application
  33. 30. Creating Frameworks
    1. Create a Framework
    2. Embed the Framework in an Application
      1. Compile the Framework for Embedding
      2. Compile the Application with the Framework
    3. Prebinding
    4. Zero-Link
  34. 31. GNUstep
    1. Creating a System That Will Run GNUstep Applications
    2. Building and Starting the Development Tools
    3. Creating the RandomApp with GNUstep
  35. 32. The End