CocoaFundamentals.pdf

(4498 KB) Pobierz
Cocoa Fundamentals Guide
Cocoa Fundamentals Guide
General
2010-12-13
695673698.001.png
Apple Inc.
© 2010 Apple Inc.
All rights reserved.
JavaisaregisteredtrademarkofOracleand/or
its affiliates.
OpenGL is a registered trademark of Silicon
Graphics, Inc.
EventhoughApplehasreviewedthisdocument,
APPLEMAKESNOWARRANTYORREPRESENTATION,
EITHEREXPRESSORIMPLIED,WITHRESPECTTO
THISDOCUMENT,ITSQUALITY,ACCURACY,
MERCHANTABILITY,ORFITNESSFORAPARTICULAR
PURPOSE.ASARESULT,THISDOCUMENTIS
PROVIDED“ASIS,”ANDYOU,THEREADER,ARE
ASSUMINGTHEENTIRERISKASTOITSQUALITY
ANDACCURACY.
INNOEVENTWILLAPPLEBELIABLEFORDIRECT,
INDIRECT,SPECIAL,INCIDENTAL,OR
CONSEQUENTIALDAMAGESRESULTINGFROMANY
DEFECTORINACCURACYINTHISDOCUMENT,even
ifadvisedofthepossibilityofsuchdamages.
THEWARRANTYANDREMEDIESSETFORTHABOVE
AREEXCLUSIVEANDINLIEUOFALLOTHERS,ORAL
ORWRITTEN,EXPRESSORIMPLIED.NoApple
dealer,agent,oremployeeisauthorizedtomake
anymodification,extension,oradditiontothis
warranty.
Somestatesdonotallowtheexclusionorlimitation
ofimpliedwarrantiesorliabilityforincidentalor
consequentialdamages,sotheabovelimitationor
exclusionmaynotapplytoyou.Thiswarrantygives
youspecificlegalrights,andyoumayalsohave
otherrightswhichvaryfromstatetostate.
Nopartofthispublicationmaybereproduced,
stored in a retrieval system, or transmitted, in
any form or by any means, mechanical,
electronic, photocopying, recording, or
otherwise, without prior written permission of
Apple Inc., with the following exceptions: Any
person is hereby authorized to store
documentation on a single computer for
personal use only and to print copies of
documentation for personal use provided that
thedocumentationcontainsApple’scopyright
notice.
The Apple logo is a trademark of Apple Inc.
No licenses, express or implied, are granted
withrespecttoanyofthetechnologydescribed
in this document. Apple retains all intellectual
propertyrightsassociatedwiththetechnology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
iAd is a service mark of Apple Inc.
Apple, the Apple logo, AppleScript, Aqua,
Bonjour, Carbon, Cocoa, Cocoa Touch, eMac,
Finder, iChat, Instruments, iPhone, iPod, iPod
touch, Mac, Mac OS, Objective-C, Quartz,
QuickTime, Safari, Spotlight, and Xcode are
trademarks of Apple Inc., registered in the
United States and other countries.
iPad and Multi-Touch are trademarks of Apple
Inc.
NeXT and NeXTSTEP are trademarks of NeXT
Software, Inc., registered in the United States
and other countries.
Adobe,Acrobat,andPostScriptaretrademarks
or registered trademarks of Adobe Systems
IncorporatedintheU.S.and/orothercountries.
IOS is a trademark or registered trademark of
CiscointheU.S.andothercountriesandisused
under license.
Intel and Intel Core are registered trademarks
of Intel Corportation or its subsidiaries in the
United States and other countries.
695673698.002.png
Contents
Introduction Introduction 11
Organization of This Document 11
See Also 12
Chapter1 WhatIsCocoa? 13
The Cocoa Environment 13
Introducing Cocoa 13
How Cocoa Fits into Mac OS X 14
How Cocoa Fits into iOS 16
Features of a Cocoa Application 18
The Development Environment 21
Platform SDKs 21
Overview of Development Workflows 21
Xcode 22
Interface Builder 24
The iOS Simulator Application 26
Performance Applications and Tools 27
The Cocoa Frameworks 29
Foundation 30
AppKit (Mac OS X) 38
UIKit (iOS) 44
Comparing AppKit and UIKit Classes 49
Core Data 50
Other Frameworks with a Cocoa API 52
A Bit of History 53
Chapter2 CocoaObjects 55
A Simple Cocoa Command-Line Tool 55
Object-Oriented Programming with Objective-C 56
The Objective-C Advantage 56
The Dynamism of Objective-C 57
Extensions to the Objective-C Language 58
Using Objective-C 64
The Root Class 66
NSObject 67
Root Class—and Protocol 67
Overview of Root-Class Methods 68
Interface Conventions 69
Instance and Class Methods 70
2010-12-13 | © 2010 Apple Inc. All Rights Reserved.
3
695673698.003.png
CONTENTS
Object Retention and Disposal 70
How the Garbage Collector Works 71
How Memory Management Works 73
Object Creation 77
Allocating an Object 77
Initializing an Object 78
The dealloc and finalize Methods 85
Class Factory Methods 86
Introspection 87
Evaluating Inheritance Relationships 87
Method Implementation and Protocol Conformance 88
Object Comparison 89
Object Mutability 90
Why Mutable and Immutable Object Variants? 90
Programming with Mutable Objects 91
Class Clusters 95
Without Class Clusters: Simple Concept but Complex Interface 95
With Class Clusters: Simple Concept and Simple Interface 96
Creating Instances 96
Class Clusters with Multiple Public Superclasses 96
Creating Subclasses Within a Class Cluster 97
Toll-Free Bridging 102
Creating a Singleton Instance 104
Chapter3 AddingBehaviortoaCocoaProgram 107
Starting Up 107
What Happens in the main Function 107
Using a Cocoa Framework 109
Kinds of Framework Classes 109
Cocoa API Conventions 110
Inheriting from a Cocoa Class 112
When to Override a Method 113
When to Make a Subclass 115
Basic Subclass Design 117
The Form of a Subclass Definition 117
Overriding Superclass Methods 119
Instance Variables 119
Entry and Exit Points 121
Initialize or Decode? 122
Storing and Accessing Properties 123
Key-Value Mechanisms 128
Object Infrastructure 129
Error Handling 130
Resource Management and Other Efficiencies 131
Functions, Constants, and Other C Types 132
4
2010-12-13 | © 2010 Apple Inc. All Rights Reserved.
CONTENTS
When the Class Is Public (Mac OS X) 132
Multithreaded Cocoa Programs 133
Multithreading and Multiprocessing Resources 133
Multithreading Guidelines for Cocoa Programs 135
Are the Cocoa Frameworks Thread Safe? 136
Chapter4 CocoaDesignPatterns 137
What Is a Design Pattern? 137
A Solution to a Problem in a Context 137
An Example: The Command Pattern 138
How Cocoa Adapts Design Patterns 139
Abstract Factory 139
Adapter 140
Chain of Responsibility 141
Command 142
Composite 143
Decorator 145
Facade 147
Iterator 148
Mediator 148
Memento 151
Observer 153
Proxy 154
Receptionist 155
Singleton 158
Template Method 158
The Model-View-Controller Design Pattern 160
Roles and Relationships of MVC Objects 160
Types of Cocoa Controller Objects 162
MVC as a Compound Design Pattern 163
Design Guidelines for MVC Applications 165
Model-View-Controller in Cocoa (Mac OS X) 167
Object Modeling 167
Entities 168
Attributes 168
Relationships 169
Accessing Properties 170
Chapter5 CommunicatingwithObjects 175
Communication in Object-Oriented Programs 175
Outlets 175
Delegates and Data Sources 176
How Delegation Works 177
The Form of Delegation Messages 178
2010-12-13 | © 2010 Apple Inc. All Rights Reserved.
5
695673698.004.png
Zgłoś jeśli naruszono regulamin