O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cocoa in a Nutshell

Book Description

Cocoa® is more than just a collection of classes, and is certainly more than a simple framework. Cocoa is a complete API set, class library, framework, and development environment for building applications and tools to run on Mac OS® X. With over 240 classes, Cocoa is divided into two essential frameworks: Foundation and Application Kit. Above all else, Cocoa is a toolkit for creating Mac OS X application interfaces, and it provides access to all of the standard Aqua® interface components such as menus, toolbars, windows, buttons, to name a few.

Cocoa in a Nutshell begins with a complete overview of Cocoa's object classes. It provides developers who may be experienced with other application toolkits the grounding they'll need to start developing Cocoa applications. Common programming tasks are described, and many chapters focus on the larger patterns in the frameworks so developers can understand the larger relationships between the classes in Cocoa, which is essential to using the framework effectively.

Cocoa in a Nutshell is divided into two parts, with the first part providing a series of overview chapters that describe specific features of the Cocoa frameworks. Information you'll find in Part I includes:

  • An overview of the Objective-C language
  • Coverage of the Foundation and Application Kit frameworks
  • Overviews of Cocoa's drawing and text handling classes
  • Network services such as hosts, Rendezvous URL services, sockets, and file handling
  • Distributed notifications and distributed objects for interapplication communication
  • Extending Cocoa applications with other frameworks, including the AddressBook, DiscRecording, and Messaging frameworks
The second half of the book is a detailed quick reference to Cocoa's Foundation and Application Kit (AppKit) classes. A complement to Apple's documentation, Cocoa in a Nutshell is the only reference to the classes, functions, types, constants, protocols, and methods that make up Cocoa's Foundation and Application Kit frameworks, based on the Jaguar release (Mac OS X 10.2).

Peer-reviewed and approved by Apple's engineers to be part of the Apple Developer Connection (ADC) Series, Cocoa in a Nutshell is the book developers will want close at hand as they work. It's the desktop quick reference they can keep by their side to look something up quickly without leaving their work.

Cocoa in a Nutshell is the book developers will want close at hand as they work. It's the desktop quick reference they can keep by their side to look something up quickly without leaving their work.

Table of Contents

  1. Preface
    1. What Is Cocoa?
      1. The Cocoa Development Environment
      2. Cocoa Design Patterns
      3. Benefits
      4. Languages
    2. How This Book Is Organized
    3. Conventions Used in This Book
    4. How the Quick Reference Was Generated
    5. Comments and Questions
    6. Acknowledgments
      1. From Mike
      2. From Duncan
  2. I. Introducing Cocoa
    1. 1. Objective-C
      1. Objects
        1. Dynamic Typing
        2. Static Typing
      2. Messaging
        1. Structure of a Message
          1. Nested messages
          2. Messaging nil
        2. How Messages Are Resolved into Methods
        3. Selectors
      3. Classes
        1. The Root Class
        2. Defining a Class
          1. The interface
          2. Scoping instance variables
          3. The implementation
        3. Special Variables
        4. Class Methods
        5. Overriding Superclass Methods
      4. Creating Object Instances
        1. Designated initializers
      5. Memory Management
        1. Retaining Objects in Accessor Methods
      6. Deallocating Objects
      7. Categories
        1. Protocols
      8. Naming Conventions
    2. 2. Foundation
      1. Data
        1. Immutable Versus Mutable Classes
        2. Basic Types
          1. Strings
          2. Comparing strings
          3. Attributed strings
          4. Working with strings: character sets and scanners
          5. NSCharacterSet
          6. NSScanner
          7. Numbers
        3. Collections
          1. Arrays
          2. Sets
          3. Dictionaries
          4. Enumerators
          5. Memory management in collections
        4. Dates and Time
        5. Binary Data
      2. Key-Value Coding
      3. Working with Files
        1. The File Manager
        2. File Handles
      4. Bundles and Resource Management
        1. Loading Resources
        2. Loading Code
      5. Archiving Objects
        1. Keyed Archiving
      6. User Defaults
      7. Notifications
      8. Operating System Interaction
        1. Process Info
        2. Tasks
      9. Threaded Programming
        1. Locks
          1. NSLock
          2. NSRecursiveLock
          3. NSConditionLock
    3. 3. The Application Kit
      1. AppKit Design Patterns
        1. Model-View-Controller
        2. Target/Action
      2. Nibs
        1. Outlets and Actions
      3. Application Architecture
        1. The Application
          1. NSApplicationMain
          2. The application delegate
          3. The run loop
        2. The Window
          1. Delegate methods
        3. The View
          1. Managing the view hierarchy
      4. Controls
        1. Cells
      5. Menus
      6. Sheets
      7. Drawers
      8. Toolbars
      9. Event Handling
        1. Event Objects
        2. The Responder Chain
      10. Document-Based Applications
    4. 4. Drawing and Imaging
      1. The Role of Quartz
      2. Coordinate Systems
      3. Graphics Contexts
      4. Working with Paths
        1. Drawing to Views
        2. Line Attributes
          1. Path flatness
          2. Line dashes and phase
          3. Line cap style
          4. Line join styles
          5. Miter limit
          6. Winding rule
      5. Drawing Text
        1. Attributed strings
      6. Working with Color
      7. Working with Images
        1. NSImage
          1. Compositing
          2. Drawing into an image
        2. NSImageRep
        3. NSBitmapImageRep
      8. Transformations
    5. 5. Text Handling
      1. Text System Architecture
        1. NSTextView
        2. NSTextStorage
        3. NSLayoutManager
        4. NSTextContainer
        5. How Text Is Laid Out
      2. Assembling the Text System
        1. Layout Scenarios
          1. Simple layout
          2. Paginating text
          3. Multicolumn text
          4. Multiple simultaneous layouts
        2. NSLayoutManager Delegation
    6. 6. Networking
      1. Hosts
      2. URL Resources
        1. Working with URLs
        2. URL Handles
          1. Managing subclasses
      3. Rendezvous Network Services
        1. NSNetService
          1. Initializing NSNetService for publication
          2. NSNetService delegate methods
          3. Errors
        2. NSNetServiceBrowser
          1. Searching for domains
      4. Sockets
      5. NSFileHandle
    7. 7. Interapplication Communication
      1. NSPipe
        1. Distributed Notifications
          1. Suspending delivery
        2. Distributed Objects
          1. DO architecture
          2. Setting up a server
          3. Connecting to a remote object
          4. DO and threads
          5. Making DO more efficient and reliable
          6. Handling communication failures
    8. 8. Other Frameworks
      1. AddressBook
        1. Working with Multiple-Value Objects
        2. Defining New Properties
        3. Searching
        4. Notifications
        5. Odds and Ends
          1. Creating a vCard from a record
          2. Adding an image to a record
      2. The Message Framework
      3. Disc Recording Frameworks
        1. The DiscRecording Framework
          1. DRBurn
          2. DRErase
          3. DRTrack
          4. Preparing audio content
          5. Preparing data content
        2. The DiscRecordingUI Framework
          1. How to record
      4. Third-Party Frameworks
  3. II. API Quick Reference
    1. 9. Foundation Types and Constants
      1. Data Types
      2. Enumerations
      3. Global Variables
      4. Constants
      5. Exceptions
    2. 10. Foundation Functions
      1. Assertions
      2. Bundles
      3. Byte Ordering
      4. Decimals
      5. Java Setup
      6. Hash Tables
      7. HFS File Types
      8. Map Tables
      9. Object Allocation
      10. Objective-C Runtime
      11. Path Utilities
      12. Points
      13. Ranges
      14. Rects
      15. Sizes
      16. Uncaught Exceptions
      17. Zones
    3. 11. Application Kit Types and Constants
      1. Data Types
      2. Enumerations
      3. Global Variables
      4. Exceptions
    4. 12. Application Kit Functions
      1. Accessibility
      2. Applications
      3. Events
      4. Fonts
      5. Graphics: General
      6. Graphics: Window Depth
      7. Interface Styles
      8. OpenGL
      9. Panels
      10. Pasteboards
      11. System Beep
    5. 13. Foundation Classes
      1. NSAppleEventDescriptor — Mac OS X 10.0
      2. NSAppleEventManager — Mac OS X 10.0
      3. NSAppleScript — Mac OS X 10.2
      4. NSArchiver — Mac OS X 10.0
      5. NSArray — Mac OS X 10.0
      6. NSAssertionHandler — Mac OS X 10.0
      7. NSAttributedString — Mac OS X 10.0
      8. NSAutoreleasePool — Mac OS X 10.0
      9. NSBundle — Mac OS X 10.0
      10. NSCalendarDate — Mac OS X 10.0
      11. NSCharacterSet — Mac OS X 10.0
      12. NSClassDescription — Mac OS X 10.0
      13. NSCloneCommand — Mac OS X 10.0
      14. NSCloseCommand — Mac OS X 10.0
      15. NSCoder — Mac OS X 10.0
      16. NSConditionLock — Mac OS X 10.0
      17. NSConnection — Mac OS X 10.0
      18. NSCountCommand — Mac OS X 10.0
      19. NSCountedSet — Mac OS X 10.0
      20. NSCreateCommand — Mac OS X 10.0
      21. NSData — Mac OS X 10.0
      22. NSDate — Mac OS X 10.0
      23. NSDateFormatter — Mac OS X 10.0
      24. NSDecimalNumber — Mac OS X 10.0
      25. NSDecimalNumberHandler — Mac OS X 10.0
      26. NSDeleteCommand — Mac OS X 10.0
      27. NSDeserializer — Mac OS X 10.0
      28. NSDictionary — Mac OS X 10.0
      29. NSDirectoryEnumerator — Mac OS X 10.0
      30. NSDistantObject — Mac OS X 10.0
      31. NSDistantObjectRequest — Mac OS X 10.0
      32. NSDistributedLock — Mac OS X 10.0
      33. NSDistributedNotificationCenter — Mac OS X 10.0
      34. NSEnumerator — Mac OS X 10.0
      35. NSException — Mac OS X 10.0
      36. NSExistsCommand — Mac OS X 10.0
      37. NSFileHandle — Mac OS X 10.0
      38. NSFileManager — Mac OS X 10.0
      39. NSFormatter — Mac OS X 10.0
      40. NSGetCommand — Mac OS X 10.0
      41. NSHost — Mac OS X 10.0
      42. NSIndexSpecifier — Mac OS X 10.0
      43. NSInvocation — Mac OS X 10.0
      44. NSKeyedArchiver — Mac OS X 10.2
      45. NSKeyedUnarchiver — Mac OS X 10.2
      46. NSLock — Mac OS X 10.0
      47. NSLogicalTest — Mac OS X 10.0
      48. NSMachBootstrapServer — Mac OS X 10.0
      49. NSMachPort — Mac OS X 10.0
      50. NSMessagePort — Mac OS X 10.0
      51. NSMessagePortNameServer — Mac OS X 10.0
      52. NSMethodSignature — Mac OS X 10.0
      53. NSMiddleSpecifier — Mac OS X 10.0
      54. NSMoveCommand — Mac OS X 10.0
      55. NSMutableArray — Mac OS X 10.0
      56. NSMutableAttributedString — Mac OS X 10.0
      57. NSMutableCharacterSet — Mac OS X 10.0
      58. NSMutableData — Mac OS X 10.0
      59. NSMutableDictionary — Mac OS X 10.0
      60. NSMutableSet — Mac OS X 10.0
      61. NSMutableString — Mac OS X 10.0
      62. NSNameSpecifier — Mac OS X 10.2
      63. NSNetService — Mac OS X 10.2
      64. NSNetServiceBrowser — Mac OS X 10.2
      65. NSNotification — Mac OS X 10.0
      66. NSNotificationCenter — Mac OS X 10.0
      67. NSNotificationQueue — Mac OS X 10.0
      68. NSNull — Mac OS X 10.0
      69. NSNumber — Mac OS X 10.0
      70. NSNumberFormatter — Mac OS X 10.0
      71. NSObject — Mac OS X 10.0
      72. NSPipe — Mac OS X 10.0
      73. NSPort — Mac OS X 10.0
      74. NSPortCoder — Mac OS X 10.0
      75. NSPortMessage — Mac OS X 10.0
      76. NSPortNameServer — Mac OS X 10.0
      77. NSPositionalSpecifier — Mac OS X 10.0
      78. NSProcessInfo — Mac OS X 10.0
      79. NSPropertyListSerialization — Mac OS X 10.2
      80. NSPropertySpecifier — Mac OS X 10.0
      81. NSProtocolChecker — Mac OS X 10.0
      82. NSProxy — Mac OS X 10.0
      83. NSQuitCommand — Mac OS X 10.0
      84. NSRandomSpecifier — Mac OS X 10.0
      85. NSRangeSpecifier — Mac OS X 10.0
      86. NSRecursiveLock — Mac OS X 10.0
      87. NSRelativeSpecifier — Mac OS X 10.0
      88. NSRunLoop — Mac OS X 10.0
      89. NSScanner — Mac OS X 10.0
      90. NSScriptClassDescription — Mac OS X 10.0
      91. NSScriptCoercionHandler — Mac OS X 10.0
      92. NSScriptCommand — Mac OS X 10.0
      93. NSScriptCommandDescription — Mac OS X 10.0
      94. NSScriptExecutionContext — Mac OS X 10.0
      95. NSScriptObjectSpecifier — Mac OS X 10.0
      96. NSScriptSuiteRegistry — Mac OS X 10.0
      97. NSScriptWhoseTest — Mac OS X 10.0
      98. NSSerializer — Mac OS X 10.0
      99. NSSet — Mac OS X 10.0
      100. NSSetCommand — Mac OS X 10.0
      101. NSSocketPort — Mac OS X 10.0
      102. NSSocketPortNameServer — Mac OS X 10.0
      103. NSSpecifierTest — Mac OS X 10.0
      104. NSSpellServer — Mac OS X 10.0
      105. NSString — Mac OS X 10.0
      106. NSTask — Mac OS X 10.0
      107. NSThread — Mac OS X 10.0
      108. NSTimer — Mac OS X 10.0
      109. NSTimeZone — Mac OS X 10.0
      110. NSUnarchiver — Mac OS X 10.0
      111. NSUndoManager — Mac OS X 10.0
      112. NSUniqueIDSpecifier — Mac OS X 10.2
      113. NSURL — Mac OS X 10.0
      114. NSURLHandle — Mac OS X 10.0
      115. NSUserDefaults — Mac OS X 10.0
      116. NSValue — Mac OS X 10.0
      117. NSWhoseSpecifier — Mac OS X 10.0
    6. 14. Foundation Protocols
      1. NSCoding — Mac OS X 10.0
      2. NSComparisonMethods — Mac OS X 10.0
      3. NSCopying — Mac OS X 10.0
      4. NSDecimalNumberBehaviors — Mac OS X 10.0
      5. NSKeyValueCoding — Mac OS X 10.0
      6. NSLocking — Mac OS X 10.0
      7. NSMutableCopying — Mac OS X 10.0
      8. NSObjCTypeSerializationCallBack — Mac OS X 10.0
      9. NSObject — Mac OS X 10.0
      10. NSScriptingComparisonMethods — Mac OS X 10.0
      11. NSScriptKeyValueCoding — Mac OS X 10.0
      12. NSScriptObjectSpecifiers — Mac OS X 10.0
      13. NSURLHandleClient — Mac OS X 10.0
    7. 15. Application Kit Classes
      1. NSActionCell — Mac OS X 10.0
      2. NSAffineTransform — Mac OS X 10.0
      3. NSApplication — Mac OS X 10.0
      4. NSBezierPath — Mac OS X 10.0
      5. NSBitmapImageRep — Mac OS X 10.0
      6. NSBox — Mac OS X 10.0
      7. NSBrowser — Mac OS X 10.0
      8. NSBrowserCell — Mac OS X 10.0
      9. NSButton — Mac OS X 10.0
      10. NSButtonCell — Mac OS X 10.0
      11. NSCachedImageRep — Mac OS X 10.0
      12. NSCell — Mac OS X 10.0
      13. NSClipView — Mac OS X 10.0
      14. NSColor — Mac OS X 10.0
      15. NSColorList — Mac OS X 10.0
      16. NSColorPanel — Mac OS X 10.0
      17. NSColorPicker — Mac OS X 10.0
      18. NSColorWell — Mac OS X 10.0
      19. NSComboBox — Mac OS X 10.0
      20. NSComboBoxCell — Mac OS X 10.0
      21. NSControl — Mac OS X 10.0
      22. NSCursor — Mac OS X 10.0
      23. NSCustomImageRep — Mac OS X 10.0
      24. NSDocument — Mac OS X 10.0
      25. NSDocumentController — Mac OS X 10.0
      26. NSDrawer — Mac OS X 10.0
      27. NSEPSImageRep — Mac OS X 10.0
      28. NSEvent — Mac OS X 10.0
      29. NSFileWrapper — Mac OS X 10.0
      30. NSFont — Mac OS X 10.0
      31. NSFontManager — Mac OS X 10.0
      32. NSFontPanel — Mac OS X 10.0
      33. NSForm — Mac OS X 10.0
      34. NSFormCell — Mac OS X 10.0
      35. NSGlyphInfo — Mac OS X 10.2
      36. NSGraphicsContext — Mac OS X 10.0
      37. NSHelpManager — Mac OS X 10.0
      38. NSImage — Mac OS X 10.0
      39. NSImageCell — Mac OS X 10.0
      40. NSImageRep — Mac OS X 10.0
      41. NSImageView — Mac OS X 10.0
      42. NSInputManager — Mac OS X 10.0
      43. NSInputServer — Mac OS X 10.0
      44. NSLayoutManager — Mac OS X 10.0
      45. NSMatrix — Mac OS X 10.0
      46. NSMenu — Mac OS X 10.0
      47. NSMenuItem — Mac OS X 10.0
      48. NSMenuItemCell — Mac OS X 10.0
      49. NSMenuView — Mac OS X 10.0
      50. NSMovie — Mac OS X 10.0
      51. NSMovieView — Mac OS X 10.0
      52. NSMutableParagraphStyle — Mac OS X 10.0
      53. NSNibConnector — Mac OS X 10.0
      54. NSNibControlConnector — Mac OS X 10.0
      55. NSNibOutletConnector — Mac OS X 10.0
      56. NSOpenGLContext — Mac OS X 10.0
      57. NSOpenGLPixelFormat — Mac OS X 10.0
      58. NSOpenGLView — Mac OS X 10.0
      59. NSOpenPanel — Mac OS X 10.0
      60. NSOutlineView — Mac OS X 10.0
      61. NSPageLayout — Mac OS X 10.0
      62. NSPanel — Mac OS X 10.0
      63. NSParagraphStyle — Mac OS X 10.0
      64. NSPasteboard — Mac OS X 10.0
      65. NSPDFImageRep — Mac OS X 10.0
      66. NSPICTImageRep — Mac OS X 10.0
      67. NSPopUpButton — Mac OS X 10.0
      68. NSPopUpButtonCell — Mac OS X 10.0
      69. NSPrinter — Mac OS X 10.0
      70. NSPrintInfo — Mac OS X 10.0
      71. NSPrintOperation — Mac OS X 10.0
      72. NSPrintPanel — Mac OS X 10.0
      73. NSProgressIndicator — Mac OS X 10.0
      74. NSQuickDrawView — Mac OS X 10.0
      75. NSResponder — Mac OS X 10.0
      76. NSRulerMarker — Mac OS X 10.0
      77. NSRulerView — Mac OS X 10.0
      78. NSSavePanel — Mac OS X 10.0
      79. NSScreen — Mac OS X 10.0
      80. NSScroller — Mac OS X 10.0
      81. NSScrollView — Mac OS X 10.0
      82. NSSecureTextField — Mac OS X 10.0
      83. NSSecureTextFieldCell — Mac OS X 10.0
      84. NSSimpleHorizontalTypesetter — Mac OS X 10.0
      85. NSSlider — Mac OS X 10.0
      86. NSSliderCell — Mac OS X 10.0
      87. NSSound — Mac OS X 10.0
      88. NSSpellChecker — Mac OS X 10.0
      89. NSSplitView — Mac OS X 10.0
      90. NSStatusBar — Mac OS X 10.0
      91. NSStatusItem — Mac OS X 10.0
      92. NSStepper — Mac OS X 10.0
      93. NSStepperCell — Mac OS X 10.0
      94. NSTableColumn — Mac OS X 10.0
      95. NSTableHeaderCell — Mac OS X 10.0
      96. NSTableHeaderView — Mac OS X 10.0
      97. NSTableView — Mac OS X 10.0
      98. NSTabView — Mac OS X 10.0
      99. NSTabViewItem — Mac OS X 10.0
      100. NSText — Mac OS X 10.0
      101. NSTextAttachment — Mac OS X 10.0
      102. NSTextAttachmentCell — Mac OS X 10.0
      103. NSTextContainer — Mac OS X 10.0
      104. NSTextField — Mac OS X 10.0
      105. NSTextFieldCell — Mac OS X 10.0
      106. NSTextStorage — Mac OS X 10.0
      107. NSTextTab — Mac OS X 10.0
      108. NSTextView — Mac OS X 10.0
      109. NSToolbar — Mac OS X 10.0
      110. NSToolbarItem — Mac OS X 10.0
      111. NSTypesetter — Mac OS X 10.0
      112. NSView — Mac OS X 10.0
      113. NSWindow — Mac OS X 10.0
      114. NSWindowController — Mac OS X 10.0
      115. NSWorkspace — Mac OS X 10.0
    8. 16. Application Kit Protocols
      1. NSAccessibility — Mac OS X 10.2
      2. NSChangeSpelling — Mac OS X 10.0
      3. NSColorPickingCustom — Mac OS X 10.0
      4. NSColorPickingDefault — Mac OS X 10.0
      5. NSComboBoxCellDataSource — Mac OS X 10.0
      6. NSComboBoxDataSource — Mac OS X 10.0
      7. NSDraggingDestination — Mac OS X 10.0
      8. NSDraggingInfo — Mac OS X 10.0
      9. NSDraggingSource — Mac OS X 10.0
      10. NSIgnoreMisspelledWords — Mac OS X 10.0
      11. NSInputServerMouseTracker — Mac OS X 10.0
      12. NSInputServiceProvider — Mac OS X 10.0
      13. NSMenuItem — Mac OS X 10.0
      14. NSMenuValidation — Mac OS X 10.0
      15. NSNibAwaking — Mac OS X 10.0
      16. NSOutlineViewDataSource — Mac OS X 10.0
      17. NSServicesRequests — Mac OS X 10.0
      18. NSTableDataSource — Mac OS X 10.0
      19. NSTextAttachmentCell — Mac OS X 10.0
      20. NSTextInput — Mac OS X 10.0
      21. NSTextStorageScripting — Mac OS X 10.0
      22. NSToolbarItemValidation — Mac OS X 10.0
      23. NSToolTipOwner — Mac OS X 10.0
      24. NSUserInterfaceValidations — Mac OS X 10.0
      25. NSValidatedUserInterfaceItem — Mac OS X 10.0
      26. NSWindowScripting — Mac OS X 10.0
    9. Method Index
  4. III. Appendix
    1. A. Appendix: Resources for Cocoa Developers
      1. Apple Documentation
      2. Related Books
      3. Web Sites
      4. Mailing Lists
      5. Partnering with Apple
  5. Index
  6. About the Authors
  7. Colophon
  8. Copyright