Cocoa Fundamentals Guide
Total Page:16
File Type:pdf, Size:1020Kb
Cocoa Fundamentals Guide 2006-10-03 registered in the United States and other Apple Computer, Inc. countries. © 2006 Apple Computer, Inc. Adobe, Acrobat, and PostScript are All rights reserved. trademarks or registered trademarks of Adobe Systems Incorporated in the U.S. No part of this publication may be and/or other countries. reproduced, stored in a retrieval system, or transmitted, in any form or by any means, Intel and Intel Core are registered mechanical, electronic, photocopying, trademarks of Intel Corportation or its recording, or otherwise, without prior subsidiaries in the United States and other written permission of Apple Computer, Inc., countries. with the following exceptions: Any person Java and all Java-based trademarks are is hereby authorized to store documentation trademarks or registered trademarks of Sun on a single computer for personal use only Microsystems, Inc. in the U.S. and other and to print copies of documentation for countries. personal use provided that the OpenGL is a registered trademark of Silicon documentation contains Apple’s copyright Graphics, Inc. notice. Simultaneously published in the United The Apple logo is a trademark of Apple States and Canada. Computer, Inc. Even though Apple has reviewed this document, Use of the “keyboard” Apple logo APPLE MAKES NO WARRANTY OR (Option-Shift-K) for commercial purposes REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS without the prior written consent of Apple DOCUMENT, ITS QUALITY, ACCURACY, may constitute trademark infringement and MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. AS A RESULT, THIS unfair competition in violation of federal DOCUMENT IS PROVIDED “AS IS,” AND and state laws. YOU, THE READER, ARE ASSUMING THE ENTIRE RISK AS TO ITS QUALITY AND No licenses, express or implied, are granted ACCURACY. with respect to any of the technology IN NO EVENT WILL APPLE BE LIABLE FOR described in this document. Apple retains DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES all intellectual property rights associated RESULTING FROM ANY DEFECT OR with the technology described in this INACCURACY IN THIS DOCUMENT, even if advised of the possibility of such damages. document. This document is intended to THE WARRANTY AND REMEDIES SET assist application developers to develop FORTH ABOVE ARE EXCLUSIVE AND IN applications only for Apple-labeled or LIEU OF ALL OTHERS, ORAL OR WRITTEN, Apple-licensed computers. EXPRESS OR IMPLIED. No Apple dealer, agent, or employee is authorized to make any Every effort has been made to ensure that modification, extension, or addition to this warranty. the information in this document is Some states do not allow the exclusion or accurate. Apple is not responsible for limitation of implied warranties or liability for typographical errors. incidental or consequential damages, so the above limitation or exclusion may not apply to Apple Computer, Inc. you. This warranty gives you specific legal rights, and you may also have other rights which 1 Infinite Loop vary from state to state. Cupertino, CA 95014 408-996-1010 Apple, the Apple logo, AppleScript, AppleScript Studio, Aqua, Bonjour, Carbon, Cocoa, iPhoto, Mac, Mac OS, Macintosh, OpenDoc, Quartz, QuickDraw, QuickTime, and Xcode are trademarks of Apple Computer, Inc., registered in the United States and other countries. Finder, Safari, and Spotlight are trademarks of Apple Computer, Inc. NeXT, NeXTSTEP, and Objective-C are trademarks of NeXT Software, Inc., Contents Introduction Introduction to Cocoa Fundamentals Guide 13 Organization of This Document 13 See Also 14 Chapter 1 What Is Cocoa? 15 The Cocoa Environment 15 Introducing Cocoa 15 How Cocoa Fits Into Mac OS X 16 Features of a Cocoa Application 19 The Development Environment 21 Xcode 21 Interface Builder 23 AppleScript Studio 25 Other Development Tools 26 The Cocoa Frameworks 27 Foundation 28 Application Kit 34 Other Frameworks With Cocoa API 40 A Bit of History 41 Chapter 2 Cocoa Objects 43 A Simple Cocoa Command-Line Tool 43 Object-Oriented Programming With Objective-C 44 The Objective-C Advantage 45 Using Objective-C 49 The Root Class 52 NSObject 52 Root Class—and Protocol 53 Overview of Root-Class Methods 53 Interface Conventions 55 Instance and Class Methods 55 The Life Cycle of a Cocoa Object 55 Object Creation 60 Allocating an Object 60 Initializing an Object 61 3 2006-10-03 | © 2006 Apple Computer, Inc. All Rights Reserved. CONTENTS The dealloc Method 68 Class Factory Methods 68 Introspection 69 Evaluating Inheritance Relationships 69 Method Implementation and Protocol Conformance 70 Object Comparison 71 Object Mutability 72 Why Mutable and Immutable Object Variants? 72 Programming With Mutable Objects 74 Class Clusters 77 Simple Concept, Complex Interface 77 Simple Concept, Simple Interface 78 Creating Instances 78 Class Clusters With Multiple Public Superclasses 79 Creating Subclasses Within a Class Cluster 80 Creating a Singleton Instance 85 Chapter 3 Adding Behavior to a Cocoa Program 87 Starting Up 87 What Happens in the main Function 87 Using a Cocoa Framework 89 Inheriting From a Cocoa Class 90 When to Override a Method 91 When to Make a Subclass 93 Basic Subclass Design 95 The Form of a Subclass Definition 95 Overriding Superclass Methods 97 Instance Variables 97 Entry and Exit Points 99 Initialize or Decode? 100 Accessor Methods 101 Key-Value Mechanisms 103 Object Infrastructure 104 Error Handling 105 Resource Management and Other Efficiencies 106 Functions, Constants, and Other C Types 107 When the Class Is Public 107 Chapter 4 Cocoa Design Patterns 109 What Is a Design Pattern? 109 A Solution to a Problem in a Context 109 An Example: The Command Pattern 110 How Cocoa Adapts Design Patterns 111 Abstract Factory 112 4 2006-10-03 | © 2006 Apple Computer, Inc. All Rights Reserved. CONTENTS Adapter 112 Chain of Responsibility 113 Command 114 Composite 115 Decorator 117 Facade 119 Iterator 120 Mediator 120 Memento 122 Observer 123 Proxy 125 Singleton 126 Template Method 126 The Model-View-Controller Design Pattern 128 Roles and Relationships of MVC Objects 128 Types of Cocoa Controller Objects 130 MVC as a Compound Design Pattern 131 Design Guidelines for MVC Applications 134 Model-View-Controller in Cocoa 135 Object Modeling 136 Entities 136 Attributes 137 Relationships 137 Accessing Properties 139 Chapter 5 Communicating With Objects 143 Communication in Object-Oriented Programs 143 Outlets 144 Delegates and Data Sources 145 How Delegation Works 145 The Form of Delegation Messages 146 Delegation and the Application Kit 148 Data Sources 148 Implementing a Delegate for a Custom Class 149 The Target-Action Mechanism 150 Controls, Cells, and Menu Items 150 The Target 151 The Action 152 Actions Defined by the Application Kit 153 Setting the Target and Action 153 Bindings 155 How Bindings Work 155 How You Establish Bindings 156 Notifications 158 When and How to Use Notifications 159 5 2006-10-03 | © 2006 Apple Computer, Inc. All Rights Reserved. CONTENTS The Notification Object 161 Notification Centers 161 Notification Queues 162 Ownership of Delegates, Observers, and Targets 165 Chapter 6 The Core Application Architecture 167 The Event-Drawing Cycle, Revisited 167 The Global Application Object 168 The Main Event Loop 168 More About Event Dispatch 170 Window Management 170 Handling Apple Events 171 Windows 172 The Windows of an Application 172 NSWindow and the Window Server 173 Window Buffering 173 Window Z-Order and Levels 174 Parts of a Window 176 Window Coordinates 177 Windows and Drawing 178 Window Status 179 Windows and Event Handling 180 Panels 181 Views 181 Varieties of Views 181 The View Hierarchy 182 View Geometry and Coordinates 184 How Views Get Drawn 188 Views and Printing 192 Views and Events 192 Responders and the Responder Chain 193 Controls and Menus 195 Control and Cell Architecture 195 Menu Characteristics and Architecture 198 Represented Objects 199 Nib Files and Other Application Resources 200 Object Archives 200 Nib Files 201 Loading Application Resources 203 Chapter 7 Other Cocoa Architectures 205 Document Architecture 205 Application Scriptability 207 Core Data 209 6 2006-10-03 | © 2006 Apple Computer, Inc. All Rights Reserved. CONTENTS Document Revision History 211 Index 213 7 2006-10-03 | © 2006 Apple Computer, Inc. All Rights Reserved. CONTENTS 8 2006-10-03 | © 2006 Apple Computer, Inc. All Rights Reserved. Figures, Tables, and Listings Chapter 1 What Is Cocoa? 15 Figure 1-1 Mac OS X architecture—simplified perspective 16 Figure 1-2 Cocoa in the architecture of Mac OS X 17 Figure 1-3 Zooming in on the Cocoa architecture—some major dependencies 17 Figure 1-4 The TextEdit example project in Xcode 22 Figure 1-5 TextEdit’s Preferences window in Interface Builder 24 Figure 1-6 The Foundation class hierarchy—Objective-C (part one) 30 Figure 1-7 Foundation class hierarchy—Objective-C (part two) 31 Figure 1-8 Foundation class hierarchy—Objective-C (part three) 32 Figure 1-9 Application Kit class hierarchy—Objective-C (part one) 35 Figure 1-10 Application Kit class hierarchy—Objective-C (part two) 36 Figure 1-11 Application Kit class hierarchy in 1988 42 Chapter 2 Cocoa Objects 43 Figure 2-1 An object’s isa pointer 45 Figure 2-2 Message terminology 51 Figure 2-3 The life cycle of an object—simplified view 57 Figure 2-4 Retaining a received object 57 Figure 2-5 Copying a received object 58 Figure 2-6 An autorelease pool 59 Figure 2-7 Initialization up the inheritance chain 65 Figure 2-8 Interactions of secondary and designated initializers 67 Figure