Cocoa Programming for Mac OS X, Third Edition

Book description

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® Programming for Mac® 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.

Table of contents

  1. Copyright
    1. Dedication
  2. Preface
  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 Library Window
      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. Sending Messages to nil
      2. NSObject, NSArray, NSMutableArray, and NSString
        1. NSObject
        2. NSArray
        3. NSMutableArray
        4. NSString
      3. “Inherits from” versus “Uses” or “Knows About”
    3. Creating Your Own Classes
      1. Creating the LotteryEntry Class
        1. LotteryEntry.h
        2. LotteryEntry.m
      2. Changing lottery.m
      3. Implementing a description Method
        1. NSCalendarDate
      4. Writing Initializers
      5. Initializers with Arguments
    4. The Debugger
    5. For the More Curious: How Does Messaging Work?
    6. Challenge
  7. 4. Memory Management
    1. Turning the Garbage Collector On and Off
    2. Living with the Garbage Collector
    3. Living with Retain Counts
      1. Implementing dealloc
      2. Creating Autoreleased Objects
        1. Temporary Objects
      3. Accessor Methods
    4. What Have You Done?
  8. 5. Target/Action
    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
      2. NSWindow’s initialFirstResponder Outlet
    4. Implementing the AppController Class
    5. For the More Curious: Setting the Target Programmatically
    6. Challenge
    7. Debugging Hints
  9. 6. Helper Objects
    1. Delegates
    2. The NSTableView and Its dataSource
    3. Lay Out the User Interface
    4. Make Connections
    5. Edit AppController.m
      1. Common Errors in Implementing a Delegate
      2. Object Delegates
    6. For the More Curious: How Delegates Work
    7. Challenge: Make a Delegate
    8. Challenge: Make a Data Source
  10. 7. Key-Value Coding; Key-Value Observing
    1. Key-Value Coding
    2. Bindings
    3. Key-Value Observing
    4. Making Keys Observable
    5. Properties and Their Attributes
      1. @property and @synthesize
      2. Attributes of a Property
    6. For the More Curious: Key Paths
    7. For the More Curious: Key-Value Observing
  11. 8. NSArrayController
    1. Starting the RaiseMan Application
      1. In Xcode
      2. In Interface Builder
    2. Key-Value Coding and nil
    3. Add Sorting
    4. For the More Curious: Sorting without NSArrayController
    5. Challenge 1
    6. Challenge 2
  12. 9. NSUndoManager
    1. NSInvocation
    2. How the NSUndoManager Works
    3. Adding Undo to RaiseMan
    4. Key-Value Observing
    5. Undo for Edits
    6. Begin Editing on Insert
    7. For the More Curious: Windows and the Undo Manager
  13. 10. 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: Creating a Protocol
    8. For the More Curious: Document-Based Applications without Undo
    9. Universal Type Identifiers
  14. 11. Basic Core Data
    1. NSManagedObjectModel
    2. Interface
      1. Create and Configure Views
      2. Connections and Bindings
    3. How Core Data Works
  15. 12. Nib Files and NSWindowController
    1. NSPanel
    2. Adding a Panel to the Application
      1. Setting Up the Menu Item
      2. AppController.m
      3. Preferences.nib
        1. File’s Owner
        2. Lay Out the User Interface
      4. PreferenceController.m
    3. For the More Curious: NSBundle
    4. Challenge
  16. 13. 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. Letting 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: NSUserDefaultsController
    9. For the More Curious: Reading and Writing Defaults from the Command Line
    10. Challenge
  17. 14. Using Notifications
    1. What Notifications Are
    2. What Notifications Are Not
    3. NSNotification and NSNotificationCenter
    4. Posting a Notification
    5. Registering as an Observer
    6. Handling the Notification When It Arrives
    7. The userInfo Dictionary
    8. For the More Curious: Delegates and Notifications
    9. Challenge
  18. 15. Using Alert Panels
    1. Make the User Confirm the Deletion
    2. Challenge
  19. 16. Localization
    1. Localizing a Nib File
    2. String Tables
      1. Creating String Tables
      2. Using the String Table
    3. For the More Curious: ibtool
    4. For the More Curious: Explicit Ordering of Tokens in Format Strings
  20. 17. Custom Views
    1. The View Hierarchy
    2. Getting a View to Draw Itself
      1. Create an Instance of a View Subclass
      2. Size Inspector
      3. drawRect
    3. Drawing with NSBezierPath
    4. NSScrollView
    5. Creating Views Programmatically
    6. For the More Curious: Cells
    7. For the More Curious: isFlipped
    8. Challenge
  21. 18. Images and Mouse Events
    1. NSResponder
    2. NSEvent
    3. Getting Mouse Events
    4. Using NSOpenPanel
      1. Change the Nib File
      2. 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
  22. 19. Keyboard Events
    1. NSResponder
    2. NSEvent
    3. Create a New Project with a Custom View
      1. Lay Out the Interface
      2. Make Connections
      3. Write the Code
        1. In BigLetterView.h
        2. In BigLetterView.m
    4. For the More Curious: Rollovers
    5. The Fuzzy Blue Box
  23. 20. Drawing Text with Attributes
    1. 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
  24. 21. Pasteboards and Nil-Targeted Actions
    1. NSPasteboard
    2. Add Cut, Copy, and Paste to BigLetterView
    3. Nil-Targeted Actions
      1. How the Responder Chain Is Searched
      2. Looking at the Nib File
    4. For the More Curious: Which Object Sends the Action Message?
    5. For the More Curious: Lazy Copying
    6. Challenge 1
    7. Challenge 2
  25. 22. Categories
    1. Add a Method to NSString
    2. For the More Curious: Declaring Private Methods
    3. For the More Curious: Declaring Informal Protocols
  26. 23. Drag-and-Drop
    1. Make BigLetterView a Drag Source
    2. Make BigLetterView a Drag Destination
      1. registerForDraggedTypes
      2. Add Highlighting
      3. Implement the Dragging-Destination Methods
      4. Test
    3. For the More Curious: Operation Mask
  27. 24. NSTimer
    1. Lay Out the Interface
    2. Make Connections
    3. Adding Code to AppController
    4. For the More Curious: NSRunLoop
    5. Challenge
  28. 25. 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
  29. 26. Creating NSFormatters
    1. A Basic Formatter
      1. Create ColorFormatter.h
      2. Edit the Nib File
      3. NSColorList
      4. Search Strings for Substrings
      5. Implement the Basic Formatter Methods
    2. The delegate of the NSControl
    3. Checking Partial Strings
    4. Formatters That Return Attributed Strings
  30. 27. Printing
    1. Dealing with Pagination
    2. For the More Curious: Am I Drawing to the Screen?
    3. Challenge
  31. 28. Web Service
    1. AmaZone
    2. Lay Out the Interface
    3. Write Code
    4. Challenge: Add a WebView
  32. 29. View Swapping
    1. Design
      1. Get Started
      2. Create ManagingViewController Class
      3. Create ViewControllers and Their NIB files
      4. Add View Swapping to MyDocument
    2. Resizing the Window
  33. 30. Core Data Relationships
    1. Edit the Model
    2. Create Custom NSManagedObject Classes
      1. Employee
      2. Department
    3. Lay Out the Interface
      1. DepartmentView.nib
      2. EmployeeView.nib
    4. Events and nextResponder
  34. 31. Garbage Collection
    1. Non-object Data Types
      1. C Primitive
      2. Core Foundation
    2. Polynomials Example
    3. Instruments
    4. For the More Curious: Weak References
    5. Challenge: Do Bad Things
  35. 32. Core Animation
    1. Creating CALayer
    2. Using CALayer and CAAnimation
      1. Removing Polynomials
      2. Moving Many Layers Simultaneously
      3. Resizing and Redrawing the Layers
      4. CALayer
  36. 33. A Simple Cocoa/OpenGL Application
    1. Using NSOpenGLView
    2. Writing the Application
      1. Lay Out the Interface
      2. Write Code
  37. 34. NSTask
    1. Multithreading versus Multiprocessing
    2. ZIPspector
    3. Asynchronous Reads
    4. iPing
    5. Challenge: .tar and .tgz files
  38. 35. The End
    1. Challenge

Product information

  • Title: Cocoa Programming for Mac OS X, Third Edition
  • Author(s):
  • Release date: May 2008
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780321562739