Você está na página 1de 20

Table Of Contents

Cocoa Programming
Scott Anguish, Erik M. Buck, Donald A. Yacktman
For more information, or to order: http://www.cocoaprogramming.net/

Also available from Amazon.com


ISBN: 0672322307
List Price: $59.99
Publisher: Sams
1st edition (September 16, 2002)
Paperback: 1272 Pages

00 0672322307_FM

8/19/02

2:10 PM

Page v

Table of Contents
Introduction

Intended Audience ......................................................................................1


Conventions ................................................................................................2
Learn By Example ........................................................................................2
Part I
1

Overview
Cocoa and Mac OS X

Understanding When to Use Cocoa ...........................................................6


Carbon ................................................................................................6
Java .....................................................................................................7
Cocoa ..................................................................................................7
Understanding Cocoas Role in Mac OS X .................................................8
Quartz .................................................................................................9
OpenGL ............................................................................................10
QuickTime ........................................................................................10
Darwin ..............................................................................................10
What You Need to Use Cocoa ...................................................................13
Whats Included in Cocoa .........................................................................13
Summary ....................................................................................................14
2

Cocoa Language Options

15

Object Orientation ....................................................................................16


Encapsulation ...................................................................................16
Modularity ........................................................................................17
Classes ...............................................................................................17
Instances ...........................................................................................17
Objects ..............................................................................................18
Specialization ....................................................................................18
Inheritance .......................................................................................18
Messages ...........................................................................................19
Polymorphism ..................................................................................19
Java .............................................................................................................19
Automatic Garbage Collection .........................................................20
Interfaces ..........................................................................................20

00 0672322307_FM

vi

8/19/02

2:10 PM

Page vi

Cocoa Programming

Security and Safety ...........................................................................20


The Java Bridge .................................................................................20
100% Pure Java .................................................................................21
JavaBeans ..........................................................................................21
Objective-C ................................................................................................21
Categories .........................................................................................21
Protocols ...........................................................................................22
Perform .............................................................................................22
Posing ...............................................................................................22
Runtime ............................................................................................22
Other Languages ........................................................................................22
ANSI C and C++ ...............................................................................23
Scripting Languages .........................................................................23
Choosing a Language for Use with Cocoa ................................................23
Java Pros and Cons ...........................................................................24
Objective-C Pros and Cons ..............................................................25
Scripting Language Pros and Cons ..................................................27
The Use of Objective-C in This Book ........................................................27
Summary ....................................................................................................27
3

Using Apples Developer Tools

29

Obtaining Apples Developer Tools ..........................................................29


Project Builder ...........................................................................................30
Creating a New Project ....................................................................30
Building the Hello World Project .....................................................32
Running Hello World .......................................................................33
Exploring the Project Pane ..............................................................33
Debugging with Project Builder .......................................................36
Using Project Builders Find Tool .....................................................36
Interface Builder ........................................................................................38
Creating a New Interface with Interface Builder .............................39
Configuring Objects .........................................................................44
Interface Builder Paradigms ............................................................52
Frameworks ................................................................................................59
Samples ......................................................................................................59
Application Kit Framework Samples ................................................60
Foundation Framework Samples ......................................................61
Interface Builder Samples .................................................................62
Additional Samples ..........................................................................62

00 0672322307_FM

8/19/02

2:10 PM

Page vii

Contents

vii

Terminal .....................................................................................................63
Other Tools ................................................................................................64
Summary ....................................................................................................66
4

Objective-C

67

Why Learn Objective-C? ...........................................................................67


Additions to C ...........................................................................................68
Messaging .........................................................................................68
File Naming and Importing .............................................................69
The id Type ......................................................................................70
Static Typing .....................................................................................70
Declaring a Class ..............................................................................71
Implementing a Class ......................................................................77
self and super .................................................................................78
Creating Instances ............................................................................79
Apples Extensions .....................................................................................80
Categories .........................................................................................80
Protocols ...........................................................................................82
Type Encoding ..................................................................................85
Constant-String Objects ...................................................................86
The NSObject Base Class ............................................................................87
Object Introspection ........................................................................88
The Introspective Format .................................................................89
Optimization ....................................................................................89
Object Comparison ..........................................................................90
Runtime Integration .........................................................................91
Runtime Functions ....................................................................................94
Objective-C++ ............................................................................................94
Summary ....................................................................................................95
5

Cocoa Conventions

97

Naming ......................................................................................................97
Prefixes .............................................................................................98
Capitalization and Scope .................................................................98
Underscores and Usage ....................................................................99
Additional Capitalization .................................................................99
Nouns and Verbs ............................................................................100
Class Names ....................................................................................100
Initializers ................................................................................................100

00 0672322307_FM

viii

8/19/02

2:10 PM

Page viii

Cocoa Programming

Managing Memory ..................................................................................102


Automatic Garbage Collection ......................................................102
Reference Counting ........................................................................103
Retain Cycles ..................................................................................108
Tracking Memory Problems ...........................................................108
Accessors ..................................................................................................109
Accessing Objects ...........................................................................111
Using Memory Zones ..............................................................................113
Encoding and Decoding ..........................................................................115
Encoding Types ..............................................................................116
Retaining Decoded Objects ............................................................119
Conditional Encoding ....................................................................119
Using Version Numbers .................................................................121
Using Memory Zones When Decoding .........................................123
Substituting Objects .......................................................................123
Using Alternative Techniques ........................................................123
Summary ..................................................................................................124
6

Cocoa Design Patterns

125

Understanding Design Patterns ..............................................................125


Design Pattern Terminology ..........................................................126
A Catalog of Cocoa Design Patterns .......................................................127
Model-View-Controller ..................................................................127
Class Clusters ..................................................................................131
Shared Objects ................................................................................131
Enumeration ...................................................................................133
Target/Action ..................................................................................134
Commands .....................................................................................135
Delegates .........................................................................................135
Notifications ...................................................................................137
Proxies ............................................................................................138
Facades ............................................................................................139
Prototypes .......................................................................................140
Archiving and Coding ....................................................................140
Subviews .........................................................................................141
Responder Chain ............................................................................142
Summary ..................................................................................................143

00 0672322307_FM

8/19/02

2:10 PM

Page ix

Contents

Part II
7

The Cocoa Frameworks


Foundation Framework Overview

147

Mutability ................................................................................................147
Mutable Instance Variables ............................................................148
Class Clusters ...........................................................................................151
Typed Storage ..........................................................................................151
Strings .............................................................................................152
Raw Data .........................................................................................155
Values .............................................................................................156
Dates ...............................................................................................158
Collections ...............................................................................................160
Arrays ..............................................................................................161
Dictionaries ....................................................................................162
Sets ..................................................................................................163
NSNull .............................................................................................163
Enumerators ...................................................................................164
Deep Versus Shallow Copying .......................................................165
Property Lists ...........................................................................................165
Run Loops and Timers .............................................................................167
NSRunLoop ......................................................................................167
NSTimer ..........................................................................................167
Support Types ..........................................................................................168
NSRange ..........................................................................................168
NSGeometry ....................................................................................169
String Processing ......................................................................................170
NSCharacterSet .............................................................................170
NSScanner ......................................................................................171
Regular Expressions ........................................................................173
Formatters .......................................................................................173
Bundles ....................................................................................................173
NSBundle .........................................................................................174
Localization ....................................................................................176
File System Access ....................................................................................177
NSFileHandle .................................................................................177
NSFileManager ...............................................................................179
NSFileWrapper ...............................................................................180
Defaults System .......................................................................................180
NSUserDefaults .............................................................................182

ix

00 0672322307_FM

8/19/02

2:10 PM

Page x

Cocoa Programming

Notifications ............................................................................................183
NSNotificationCenter ..................................................................184
NSNotification .............................................................................185
Related Core Foundation .........................................................................185
Summary ..................................................................................................187
8

The Application Kit Framework Overview

189

Events and the Run Loop ........................................................................190


Responders ...............................................................................................190
What Is a Responder? .....................................................................191
The Responder Chain .....................................................................192
The First Responder ........................................................................193
NSApplication Overview ........................................................................195
NSWindow Overview ..................................................................................195
Backing Store ..................................................................................196
Key Window and Main Window ...................................................197
Windows in the Responder Chain .................................................199
NSView Overview ......................................................................................199
View Hierarchy ...............................................................................199
Views in the Responder Chain ......................................................201
Delegates ..................................................................................................203
Delegation Versus Notifications .....................................................204
Specialization of Behavior and Coupling ......................................204
Delegation Versus Multiple Inheritance ........................................206
Limitations of Delegation ..............................................................206
Target-Action Paradigm ...........................................................................207
Archived Objects and Nibs ......................................................................209
Nib Awaking ...................................................................................210
The Files Owner .............................................................................211
NSWindowController Overview ...............................................................212
Multidocument Applications ..................................................................213
NSApplication Support ..................................................................214
NSDocumentController Overview ..................................................215
NSDocument Overview .....................................................................217
NSFileWrapper Overview ...............................................................218
Undo and Redo ........................................................................................218
Menu Validation ......................................................................................219

00 0672322307_FM

8/19/02

2:10 PM

Page xi

Contents

Spell Checking .........................................................................................219


Enabling Spell Checking ................................................................220
Summary ..................................................................................................221
9

Applications, Windows, and Screens

223

The New Image Viewer ............................................................................223


The Role of the MYDocument Class ..................................................224
The Role of the MYDocumentManager Class .....................................224
Working with NSWindow ...........................................................................225
NSWindows Delegate ........................................................................225
Configuring the Documents Window ..........................................227
Implementing MYDocument ............................................................232
Transparent Windows ....................................................................250
Working with NSApplication ................................................................250
NSApplications Delegate ..............................................................251
Creating and Configuring MYDocumentManager ............................253
Implementing MYDocumentManager ...............................................257
Modal Loops ............................................................................................272
Using Modal Windows ...................................................................273
Using Modal Views .........................................................................273
Working with Sheets ...............................................................................274
Changes to MYDocument to Support Sheets ....................................274
Changes to MYDocumentManager to Support Sheets .......................280
Working with Drawers .............................................................................283
Working with Screens ..............................................................................283
Working with Panels ...............................................................................284
The NSPanel Class and Subclasses .................................................284
Summary ..................................................................................................285
10

Views and Controls

287

Controls ...................................................................................................287
NSControl Class ..............................................................................288
Relationship Between Controls and Cells .....................................293
Simple Views and Controls .....................................................................296
Buttons ...........................................................................................296
Sliders .............................................................................................306
Text Fields .......................................................................................310
Image Display .................................................................................320
Progress Indicators .........................................................................323

xi

00 0672322307_FM

xii

8/19/02

2:10 PM

Page xii

Cocoa Programming

Container Views and Controls ................................................................325


Boxes ...............................................................................................326
Scroll Views ....................................................................................329
Tab Views ........................................................................................334
Split Views ......................................................................................339
Compound Controls ...............................................................................341
Steppers ..........................................................................................342
NSMatrix Class ................................................................................343
Forms ..............................................................................................348
Pop-Up Buttons and Pull-Down Lists ............................................350
Summary ..................................................................................................353
11

The Cocoa Text System

355

Using the High-Level Text Classes ..........................................................356


Setting the Text to Display .............................................................357
Appending, Inserting, and Replacing Text ....................................357
Programmatically Ending Editing ..................................................358
The Text System Architecture ..................................................................359
The Model Layer: Text Storage and Attributed Strings .................361
The Control Layer: Text Layout and Containers ...........................371
The View Layer: NSTextView ..........................................................373
Managing Fonts .......................................................................................376
The NSFont Class ............................................................................377
Using the Font Manager ................................................................379
Using the Font Panel and Font Menu ...........................................386
Text Input ................................................................................................388
Using Delegate Methods ................................................................389
Using Formatters ............................................................................393
Summary ..................................................................................................402
12

Custom Views and Graphics Part I

403

The Quartz Graphics Model ....................................................................404


Resolution Independence ..............................................................404
Device Independence .....................................................................405
Transparency ..................................................................................405
Paths ...............................................................................................405
Transforms ......................................................................................406
Bitmapped Images ..........................................................................406

00 0672322307_FM

8/19/02

2:10 PM

Page xiii

Contents

Graphics Contexts ..........................................................................407


Text Rendering ...............................................................................407
Quartz Graphics Via the Application Kit ................................................407
New Types and Functions ..............................................................408
Subclassing NSView .........................................................................413
Using the NSBezierPath Class .................................................................418
Constructing a Path .......................................................................420
Rendering a Path ............................................................................434
Drawing Shortcuts: Rectangle Functions and More ......................443
Modifying Drawing .................................................................................450
NSBezierPath Parameters ...............................................................450
Querying and Modifying a Path ....................................................457
Summary ..................................................................................................459
13

Custom Views and Graphics Part II

461

Using NSGraphicsContext .......................................................................461


Where Am I Drawing? ....................................................................462
Flushing a Context .........................................................................462
Controlling the Drawing ................................................................464
NSGraphicsContext Advanced Methods ........................................466
Coordinate System Transformations .......................................................468
Defining an NSAffineTransform ...................................................468
Using an NSAffineTransform ........................................................476
Expert Transforms ..........................................................................479
Drawing Points and Rectangles ...............................................................485
Drawing Individual Rectangles ......................................................486
Drawing Lists of Rectangles ...........................................................487
Drawing with Bitmaps ...................................................................489
Optimizing Drawing ................................................................................496
Finding Drawing Trouble Spots .....................................................498
Easy Optimizations ........................................................................503
Controlling Display and Flushing .................................................506
Caching Complex Drawing ...........................................................509
Custom Buffering ...........................................................................511
Other Optimizations ......................................................................515
Summary ..................................................................................................519

xiii

00 0672322307_FM

xiv

8/19/02

2:10 PM

Page xiv

Cocoa Programming

14

Custom Views and Graphics Part III

521

Images and Bitmaps ................................................................................521


Compositing ...................................................................................521
The NSImage Class ..........................................................................526
Manipulating Image Representations ............................................548
Drawing Text ...........................................................................................569
NSString Drawing Methods ...........................................................569
NSBezierPath and Glyphs ..............................................................574
Summary ..................................................................................................582
15

Events and Cursors

583

Event Handling in Custom NSView Subclasses .......................................583


Accepting First Responder Status ...................................................584
Detecting Changes in First Responder Status ................................586
First Mouse Clicks ..........................................................................587
Controlling Key Status and Window Ordering .............................588
The NSEvent Class ..........................................................................589
Handling Mouse Events .................................................................595
Handling Key Events ......................................................................601
Other Events ...................................................................................606
Managing Cursors ....................................................................................612
NSCursor Class ................................................................................612
Cursor Management .......................................................................616
Changing the Cursor ......................................................................617
Summary ..................................................................................................625
16

Menus

627

Standard Menu Layouts ...........................................................................628


Menu Support in Interface Builder ................................................628
Menu Support in the Cocoa APIs ..................................................630
NSMenu Class .............................................................................................630
Creating an NSMenu ..........................................................................631
Adding Items to an NSMenu ............................................................631
Removing Items from an NSMenu ...................................................632
Finding Items in an NSMenu ...........................................................632
NSMenu Notifications .......................................................................633
Other NSMenu Methods ...................................................................634
NSMenuItem Class .....................................................................................634

00 0672322307_FM

8/19/02

2:10 PM

Page xv

Contents

Creating an NSMenuItem ................................................................635


Configuring an NSMenuItem ..........................................................636
The DynamicMenu Example .............................................................639
Menu Validation ......................................................................................642
Implementing the -validateMenuItem: Method ..........................643
Disabling Automatic Validation .....................................................644
Contextual Menus ...................................................................................644
Dock Menus .............................................................................................646
Extending the Dock Menu Without Code ....................................647
Extending the Dock Menu with a Delegate ..................................648
Deprecated Functionality ........................................................................649
Summary ..................................................................................................650
17

Color

651

NSColor Class ...........................................................................................651

Creating Color Objects ...................................................................652


Setting the Current Color ..............................................................654
Querying NSColor Settings .............................................................654
Visually Representing an NSColor ..................................................656
Color Space Conversion .................................................................656
Creating Derived Colors .................................................................657
System Color Values and Notification ...........................................660
Storing and Retrieving Colors ........................................................660
Color Wells ..............................................................................................661
NSColorWell Class ..........................................................................661
Color Panels .............................................................................................663
NSColorPanel Class ........................................................................663
Dragging Colors .............................................................................666
Customizing the Color Panel ..................................................................667
Custom Color Pickers .....................................................................667
NSColorList Class ...................................................................................671
Summary ..................................................................................................673
18

Advanced Views and Controls

675

NSTableView, NSOutlineView, and NSBrowser Concepts ........................675

Data Sources ...................................................................................676


Selecting an Appropriate User Interface ........................................677
Terminology Used with Hierarchical Data ....................................678

xv

00 0672322307_FM

xvi

8/19/02

2:10 PM

Page xvi

Cocoa Programming

Table Views ..............................................................................................679


Setting Up an NSTableView ............................................................680
Custom Formatting the Cells in an NSTableView .........................684
Master-Detail Interfaces with NSTableView ...................................688
Outline Views ..........................................................................................696
The Outline Table Column ............................................................697
NSOutlineView Data Sources ..........................................................698
NSOutlineView Delegate Methods .................................................702
Other NSOutlineView Methods ......................................................703
Browsers ...................................................................................................704
Configuring an NSBrowser in Interface Builder .............................705
Important NSBrowser Methods ......................................................707
NSBrowserCell Class ......................................................................709
NSBrowser Delegates .......................................................................709
ClassBrowser Example ..................................................................712
Combo Boxes ...........................................................................................718
Combo Boxes in Interface Builder .................................................718
Other Combo Box Methods ...........................................................719
Managing a Combo Box Item List .................................................720
Selecting an Item Programmatically ..............................................721
Combo Box Delegate Methods ......................................................723
Custom Controls .....................................................................................723
Subclassing NSControl ...................................................................724
Subclassing NSCell ........................................................................725
Control Tint ...................................................................................730
Toolbars ....................................................................................................730
NSToolbar Class ..............................................................................731
Creating an NSToolbar Delegate ....................................................735
Toolbar Item Validation .................................................................738
Status Bars ................................................................................................739
Working with NSStatusBar and NSStatusItem ............................739
StatusItems Example ......................................................................741
NSQuickDrawView Class ............................................................................743
Summary ..................................................................................................744
19

Using Pasteboards

745

Pasteboard Concepts ...............................................................................745


Pasteboard Server ...........................................................................746
Pasteboard Data Types ...................................................................746

00 0672322307_FM

8/19/02

2:10 PM

Page xvii

Contents

Lazy Evaluation ..............................................................................747


Services ...........................................................................................747
Filters ..............................................................................................749
Implementing Cut, Copy, and Paste .......................................................749
Obtaining a Pasteboard ..................................................................750
Pasteboard Types ............................................................................751
Filtering an NSPasteboard .............................................................752
Writing Data to an NSPasteboard .................................................752
Providing Data Lazily .....................................................................754
Reading Data from an NSPasteboard ............................................756
Implementing Drag and Drop .................................................................758
Drag and Drop in Custom View and Window Objects .................758
Drag and Drop for Table and Outline Views .................................767
Implementing Services ............................................................................772
Using Services .................................................................................773
Invoking Services Programmatically ..............................................776
Implementing Service Providers ....................................................777
Summary ..................................................................................................788
20

Adding Online Help

791

Apple Help ...............................................................................................791


Integrating a Help Book into Your Application ............................794
Extending Help Functionality Using Carbon ................................796
ToolTips ...................................................................................................805
Using Interface Builder to Set ToolTips .........................................806
Cocoa APIs for ToolTips .................................................................806
Context-Sensitive Help (NSHelpManager) ................................................811
Summary ..................................................................................................811
21

Multimedia

813

Sound .......................................................................................................813
NSSound Class ..................................................................................813
Core Audio and Core MIDI Frameworks .......................................815
Speech Synthesis and Recognition ................................................816
QuickTime ...............................................................................................816
NSMovie Class ..................................................................................817
NSMovieView ..................................................................................818

xvii

00 0672322307_FM

xviii

8/19/02

2:10 PM

Page xviii

Cocoa Programming

3D Graphics .............................................................................................822
Application Kit Frameworks ...........................................................823
Procedural OpenGL ........................................................................829
Summary ..................................................................................................829
22

Integrating with the Operating System

831

Getting System Information ...................................................................831


NSProcessInfo Class ......................................................................831
NSHost Class ...................................................................................833
Users ...............................................................................................834
Standard Locations .........................................................................834
Authentication and Security ...................................................................837
Set uid Cocoa Applications ............................................................838
Communicating with the Workspace .....................................................839
NSWorkspace Class ..........................................................................840
Summary ..................................................................................................849
23

Networking

851

NSURL and NSURLHandle ...........................................................................851

Creating a New NSURL ....................................................................852


Decomposing an NSURL ..................................................................853
Downloading Data .........................................................................856
Email Messages ........................................................................................859
Creating a Message in the Users Email Client ..............................859
Directory Services ....................................................................................862
Interapplication Programming ................................................................862
Apple Events ...................................................................................863
Distributed Notifications ...............................................................863
Distributed Objects ........................................................................866
Low-Level Interprocess Communication .......................................879
Summary ..................................................................................................880
24

Subprocesses and Threads

881

Choosing Between Subprocesses and Threads ........................................881


Using the NSTask Class ............................................................................883
NSTask Synchronously: Calendar Example ....................................884
NSTask Asynchronously: Animal Example ....................................891

00 0672322307_FM

8/19/02

2:10 PM

Page xix

Contents

Using the NSThread Class ........................................................................902


The ThreadExample Program .........................................................903
Locking ....................................................................................................916
Using the NSLock and NSConditionLock Classes ...........................917
Using the NSDistributedLock Class ..............................................921
Threading Issues ......................................................................................923
Performance Issues .........................................................................923
Critical Sections ..............................................................................924
Global Variables .............................................................................925
Race Conditions .............................................................................926
Deadlocks .......................................................................................927
Starvation .......................................................................................928
Debugging ......................................................................................929
Using NSTimers Instead of Threads ...............................................930
Summary ..................................................................................................931
25

Printing

933

Basic Printing ...........................................................................................933


Overview of the Printing Classes ............................................................934
NSPrintInfo ..................................................................................934
NSPrinter ......................................................................................939
NSPageLayout .................................................................................939
NSPrintPanel .................................................................................942
NSPrintOperation .........................................................................943
NSViews Printing Support ......................................................................946
Additional Printing Features ..........................................................947
Printing and Pagination Example ...........................................................948
AutomaticPagination .....................................................................949
CustomPagination ..........................................................................958
AlternativePagination Technique ..................................................960
Printing in NSDocument-Based Applications ............................................961
NSPageLayout Interaction ..............................................................962
Printing Documents .......................................................................963
Summary ..................................................................................................964

xix

00 0672322307_FM

8/19/02

Part III
26

2:10 PM

Page xx

Cocoa Techniques
Application Requirements, Design, and Documentation

967

Designing an Application with Requirements ........................................968


Describing the Goal .......................................................................968
Deriving Requirements from a Description ...................................969
Selecting an Architecture ...............................................................972
Designing TransparentTetris ....................................................................972
TransparentTetris Model .................................................................973
TransparentTetris View ...................................................................981
TransparentTetris Controller ..........................................................983
Implementing the Design .......................................................................985
MYSubBlock Class .........................................................................986
MYBlock Class ...............................................................................988
MYGameModel Class ........................................................................1001
MYGameController Class ...............................................................1022
Using AutoDoc ......................................................................................1036
Summary ................................................................................................1037
27

Creating Custom Frameworks

1039

Creating and Using a Framework ..........................................................1040


Install Location ............................................................................1040
Prebinding ....................................................................................1041
Building a Custom Framework ....................................................1043
Using a Custom Framework .........................................................1043
Header Files ............................................................................................1044
Precompiled Headers ....................................................................1044
Public Versus Private API ..............................................................1045
Providing Backward Compatibility .......................................................1047
Debugging Frameworks .........................................................................1048
Summary ................................................................................................1049
28

Distributing Applications

1051

Package Directories ................................................................................1051


Using Disk Images .................................................................................1052
Creating a DMG Using Disk Copy ...............................................1053
Creating a DMG File from the Command Line ..........................1056
Customizing DMG Archives ........................................................1057

00 0672322307_FM

8/19/02

2:10 PM

Page xxi

Contents

Application Installation .........................................................................1058


Drag in the File System ................................................................1059
Installer .........................................................................................1059
Third-Party Installers ....................................................................1061
Summary ................................................................................................1062
Part IV
A

Appendixes
Unleashing the Objective-C Runtime

1065

Objective-C Objects ...............................................................................1066


The isa Element ...........................................................................1066
Extra Bytes ....................................................................................1066
Class ..............................................................................................1067
Messaging with IMPs and Selectors .......................................................1072
Searching for a Method ................................................................1074
Common Runtime Functions ...............................................................1075
Forwarding, Distributed Objects, and Proxies ......................................1076
Examples ................................................................................................1077
Get a List of All Classes ................................................................1077
Get a List of All Instance Variables ..............................................1078
Get a List of All Methods .............................................................1079
Store the IMP for a Replaced Method ...........................................1079
Access Any Class From a Scripting Language ..............................1084
Summary ................................................................................................1085
B

Optimizing and Finding Memory Leaks

1087

Optimizing Applications .......................................................................1087


Premature Optimization ..............................................................1087
The 80/20 Rule .............................................................................1088
The LotsOfWords Application ......................................................1088
First Optimize Graphics ...............................................................1101
Optimizing Nongraphical Code ...................................................1109
Finding Performance Bottlenecks ................................................1109
Optimize Only the Bottlenecks ...................................................1111
Optimizing Rules of Thumb ........................................................1116
Avoiding Dynamic Memory Allocation .......................................1117

xxi

00 0672322307_FM

xxii

8/19/02

2:10 PM

Page xxii

Cocoa Programming

Finding Memory Leaks ..........................................................................1128


ObjectAlloc ...................................................................................1128
MallocDebug ................................................................................1130
Using Cocoa Frameworks to Find Leaks ......................................1130
Summary ................................................................................................1132
C

Finding Third-Party Resources

1133

Apple-Provided Documentation ...........................................................1133


Cocoa Developer Documentation ...............................................1133
Core Foundation ..........................................................................1134
Carbon Frameworks .....................................................................1134
Developer Tools ............................................................................1134
Release Notes ................................................................................1134
Example Code ........................................................................................1135
Cocoa Sample Code .....................................................................1135
Omni Group Frameworks ............................................................1135
MiscKit Frameworks .....................................................................1136
MOKit Frameworks ......................................................................1136
EDCommon Frameworks .............................................................1137
Web Sites ................................................................................................1137
Stepwise ........................................................................................1137
Softrak ...........................................................................................1138
OReilly Mac DevCenter ...............................................................1138
CocoaDev .....................................................................................1138
MacTech ........................................................................................1138
Mailing Lists ..........................................................................................1138
cocoa-dev .....................................................................................1138
Project Builder ..............................................................................1139
macosx-dev ...................................................................................1139
Summary ................................................................................................1139
D

Cocoa Additions in Mac OS X Version 10.2

1141

Quartz Extreme ......................................................................................1141


OpenGL Accelerated 2D ...............................................................1142
Multiple Layers per Window ........................................................1142
Requirements ................................................................................1142
Handwriting Recognition ......................................................................1143
Address Book and vCard .......................................................................1143
Universal Access ....................................................................................1143

00 0672322307_FM

8/19/02

2:10 PM

Page xxiii

Contents

Updated Tools ........................................................................................1144


GCC 3.1 ........................................................................................1144
BSD Tools ......................................................................................1145
IPv6 ...............................................................................................1145
Updated Cocoa Spell Checking Support ......................................1145
Framework Enhancements ....................................................................1146
Summary ................................................................................................1146
Index

1149

xxiii

Você também pode gostar