The Objective-C Programming Language

Transcription

Inside Mac OS XThe Objective-C ProgrammingLanguageFebruary 2003

Apple Computer, Inc. 2002 Apple Computer, Inc.All rights reserved.No part of this publication may bereproduced, stored in a retrievalsystem, or transmitted, in any form orby any means, mechanical, electronic,photocopying, recording, orotherwise, without prior writtenpermission of Apple Computer, Inc.,with the following exceptions: Anyperson is hereby authorized to storedocumentation on a single computerfor personal use only and to printcopies of documentation for personaluse provided that the documentationcontains Apple’s copyright notice.The Apple logo is a trademark ofApple Computer, Inc.Use of the “keyboard” Apple logo(Option-Shift-K) for commercialpurposes without the prior writtenconsent of Apple may constitutetrademark infringement and unfaircompetition in violation of federaland state laws.No licenses, express or implied, aregranted with respect to any of thetechnology described in this book.Apple retains all intellectual propertyrights associated with the technologydescribed in this book. This book isintended to assist applicationdevelopers to develop applicationsonly for Apple-labeled orApple-licensed computers.Every effort has been made to ensurethat the information in this documentis accurate. Apple is not responsiblefor typographical errors.Apple Computer, Inc.1 Infinite LoopCupertino, CA 95014408-996-1010Apple, the Apple logo, andMacintosh are trademarks of AppleComputer, Inc., registered in theUnited States and other countries.HotSpot is a trademark of SunMicrosystems, Inc.Simultaneously published in theUnited States and Canada.Even though Apple has reviewed thismanual, APPLE MAKES NOWARRANTY OR REPRESENTATION,EITHER EXPRESS OR IMPLIED, WITHRESPECT TO THIS MANUAL, ITSQUALITY, ACCURACY,MERCHANTABILITY, OR FITNESSFOR A PARTICULAR PURPOSE. AS ARESULT, THIS MANUAL IS SOLD “ASIS,” AND YOU, THE PURCHASER, AREASSUMING THE ENTIRE RISK AS TOITS QUALITY AND ACCURACY.IN NO EVENT WILL APPLE BE LIABLEFOR DIRECT, INDIRECT, SPECIAL,INCIDENTAL, OR CONSEQUENTIALDAMAGES RESULTING FROM ANYDEFECT OR INACCURACY IN THISMANUAL, even if advised of thepossibility of such damages.THE WARRANTY AND REMEDIES SETFORTH ABOVE ARE EXCLUSIVE ANDIN LIEU OF ALL OTHERS, ORAL ORWRITTEN, EXPRESS OR IMPLIED. NoApple dealer, agent, or employee isauthorized to make any modification,extension, or addition to this warranty.Some states do not allow the exclusion orlimitation of implied warranties orliability for incidental or consequentialdamages, so the above limitation orexclusion may not apply to you. Thiswarranty gives you specific legal rights,and you may also have other rights whichvary from state to state.

ContentsFigures, Listings, and TablesChapter 111IntroductionThe Development Environment14Why Objective-C15How This Book is Organized16Conventions17Chapter 2Object-Oriented ProgrammingInterface and Implementation20The Object Model24The Messaging isms Of nce35Class Hierarchies35Subclass Definitions36Uses of Inheritance37Dynamism39Dynamic Typing39Dynamic Binding40Dynamic Loading43Structuring Programs44Outlet Connections453 Apple Computer, Inc. February 2003

C O N T E N T SExtrinsic and Intrinsic Connections47Activating the Object Network47Aggregation and Decomposition48Models and Frameworks49Structuring the Programming Task50Collaboration51Organizing Object-Oriented Projects52Designing on a Large Scale52Separating the Interface from the ImplementationModularizing the Work52Keeping the Interface Simple53Making Decisions Dynamically53Inheriting Generic Code53Reusing Tested Code54Chapter 3The Objective-C LanguageObjective-C Objects55id56Dynamic Typing57Object Messaging58The Receiver’s Instance Variables59Polymorphism60Dynamic Binding60Classes62Inheritance62The NSObject Class64Inheriting Instance Variables64Inheriting Methods65Overriding One Method With AnotherAbstract Classes66Class Types67Static Typing67Type Introspection68Class Objects68Creating Instances70Customization With Class Objects714 Apple Computer, Inc. February 20036652

C O N T E N T SVariables and Class Objects72Initializing a Class Object73Methods of the Root Class74Class Names in Source Code74Defining a Class75The Interface76Importing the Interface78Referring to Other Classes79The Role of the Interface79The Implementation80Referring to Instance Variables82The Scope of Instance Variables83How Messaging Works87Selectors90Methods and Selectors91Method Return and Argument Types91Varying the Message at Runtime92The Target-Action Paradigm92Avoiding Messaging Errors94Hidden Arguments94Messages to self and super95An Example96Using super99Redefining self99Extending Classes101Categories—Adding Methods to Existing Classes101Adding to a Class102How Categories Are Used103Categories of the Root Class103Protocols—Declaring Interfaces for Others to ImplementWhen to Use Protocols105Enabling Static Behaviors115Static Typing116Type Checking117Return and Argument Types118Static Typing to an Inherited Class118Getting a Method Address120Getting an Object Data Structure1201045 Apple Computer, Inc. February 2003

C O N T E N T SUsing C With Objective-C122Mixing Objective-C and C Language FeaturesC Lexical Ambiguities and Conflicts125Chapter 4122The Objective-C Runtime SystemMemory Management129Allocating and Initializing Objects129The Returned Object130Arguments131Coordinating Classes132The Designated Initializer134Combining Allocation and Initialization139Retaining Objects140Handling Cyclical References141Deallocation142Releasing Shared Objects143Releasing Instance Variables144Marking Objects for Later Release144Object Ownership145Forwarding146Forwarding and Multiple Inheritance149Surrogate Objects150Forwarding and Inheritance150Dynamic Loading152Remote Messaging152Distributed Objects153Language Support155Synchronous and Asynchronous Messages156Pointer Arguments157Proxies and Copies159Type Encodings160Chapter 5Objective-C Runtime Functions and Data StructuresObjective-C Functions1656 Apple Computer, Inc. February 2003

C O N T E N T SAccessing Selectors166sel getName166sel isMapped167sel registerName167sel getUid168Sending Messages168objc msgSend169objc msgSend stret169objc msgSendSuper170objc msgSendSuper stret171Forwarding Messages172objc msgSendv172objc msgSendv stret173marg malloc173marg free174marg getRef174marg getValue174marg setValue175Adding Classes175objc addClass175Accessing Methods178class getInstanceMethod178class getClassMethod179class nextMethodList179class addMethods180class removeMethods181method getNumberOfArguments182method getSizeOfArguments182method getArgumentInfo182Accessing Instance Variable Definitions183class getInstanceVariable183Accessing the Class Version183class setVersion183class getVersion184Posing As Another Class185class poseAs185Obtaining Class Definitions185objc getClassList1867 Apple Computer, Inc. February 2003

C O N T E N T Sobjc getClass187objc lookUpClass188objc getMetaClass188objc setClassHandler189Instantiating Classes189class createInstance189class createInstanceFromZone190Accessing Instance Variables190object setInstanceVariable191object getInstanceVariable191Objective-C Callbacks192Class Handler Callback192Objective-C Data Types192Class Definition Data Structures193objc class194objc ivar196objc ivar list197objc method198objc method list199objc cache200objc protocol list201Instance Data Types201objc object202objc super202Appendix AObjective-C Language SummaryMessages205Defined Types206Preprocessor Directives207Compiler Directives207Classes208Categories209Formal Protocols210Method Declarations211Method Implementations212Naming Conventions2128 Apple Computer, Inc. February 2003

C O N T E N T SAppendix BGrammar for the Objective-C LanguageExternal Declarations217Type Specifiers221Type Qualifiers222Primary Expressions222Chapter CDocument Revision HistoryGlossaryIndex2272339 Apple Computer, Inc. February 2003

C O N T E N T S10 Apple Computer, Inc. February 2003

Figures, Listings, and TablesChapter 2Object-Oriented ProgrammingFigure 2-1Figure 2-2Figure 2-3Figure 2-4Figure 2-5Chapter 321The Objective-C LanguageFigure 3-1Figure 3-2Figure 3-3Figure 3-4Figure 3-5Figure 3-6Listing 3-1Chapter 4Interface and ImplementationAn Object24Object Network26Inheritance Hierarchy36Outlets46Some Drawing Program Classes63Rectangle Instance Variables65Inheritance Hierarchy for NSCells71The Scope of Instance Variables85Messaging Framework89High, Mid, Low97Using C and Objective-C instances as instance variables122The Objective-C Runtime SystemFigure 4-1Figure 4-2Figure 4-3Figure 4-4Figure 4-5Figure 4-6Figure 4-7Figure 4-8Table 4-1Table 4-2Incorporating an Inherited Initialization MethodCovering an Inherited Initialization Model134Covering the Designated Initializer136Initialization Chain138Retaining Objects142Forwarding149Remote Messages154Round-Trip Message156Objective-C type encodings161Objective-C method encodings16313311 Apple Computer, Inc. February 2003

F I G U R E SChapter 5T A B L E SObjective-C Runtime Functions and Data StructuresListing 5-1Listing 5-2Listing 5-3Chapter CA N DCreating an Objective-C class definition176Obtaining class method definitions180Using objc getClassList186Document Revision HistoryTable 5-1Document Revision History12 Apple Computer, Inc. February 2003225

C H A P T E R11IntroductionThe Objective-C language is a simple computer language designed to enablesophisticated object-oriented programming.Object-oriented programming, like most interesting developments, builds on someold ideas, extends them, and puts them together in novel ways. The result ismany-faceted and a clear step forward for the art of programming. Anobject-oriented approach makes programs more intuitive to design, faster todevelop, more amenable to modifications, and easier to understand. It leads notonly to alternative ways of constructing programs, but also to alternative ways ofconceiving the programming task.Nevertheless, for those who have never used object-oriented programming tocreate applications before, object-oriented programming may present someformidable obstacles. It introduces a new way of doing things that may seemstrange at first, and it comes with an extensive terminology that can take somegetting used to. The terminology will help in the end, but it’s not always easy tolearn. It can be difficult to get started.That’s where this book comes in. It fully documents the Objective-C language, anobject-oriented programming language based on standard C, and provides afoundation for learning about Mac OS X’s Objective-C application developmentframework—Cocoa.This book is also designed to help you become familiar with object-orientedprogramming and get over the hurdle its terminology presents. It spells out someof the implications of object-oriented design and tries to give you a flavor ofwhat writing an object-oriented program is really like.The book is intended for readers who might be interested in: Learning about object-oriented programming,13 Apple Computer, Inc. February 2003

C H A P T E R1Introduction Finding out about the basis for the Cocoa application framework, or Programming in Objective-C.The Development EnvironmentMost object-oriented development environments consists of at least three parts: A library of objects A set of development tools An object-oriented programming language and support libraryCocoa is an extensive library. It includes several software frameworks containingdefinitions for objects that you can use “off the shelf” or adapt to your program’sneeds. These include the Foundation Framework, the Application Kit Framework(for building a graphical user interface), and others.Mac OS X also includes development tools for putting together applications.There’s Interface Builder, a program that lets you design an application graphicallyand assemble its user interface on-screen, and Project Builder, aproject-management program that provides graphical access to the compiler, thedebugger, documentation, a program editor, and other tools.This book is about the third component of the development environment—theprogramming language and its runtime environment. All Cocoa frameworks arewritten in the Objective-C language. To get the benefit of the frameworks,applications must use either Objective-C or a language bridged to Objective-C, suchas Java.Objective-C is defined as set of extensions to the C language. It’s designed to give Cfull object-oriented programming capabilities, and to do so in a simple andstraightforward way. Its additions to C are few and are mostly based on Smalltalk,one of the first object-oriented programming languages.This book both introduces the object-oriented model that Objective-C is based uponand fully documents the language. It concentrates on the Objective-C extensions toC, not on the C language itself. There are many good books available on C; this bookdoesn’t attempt to duplicate them.14 Apple Computer, Inc. February 2003

C H A P T E R1IntroductionBecause this isn’t a book about C, it assumes some prior acquaintance with thatlanguage. However, it doesn’t have to be an extensive acquaintance.Object-oriented programming in Objective-C is sufficiently different fromprocedural programming in standard C that you won’t be hampered if you’re notan experienced C programmer.Why Objective-CThe Objective-C language was chosen for the Cocoa framework for a variety ofreasons. First and foremost, it’s an object-oriented language. The kind offunctionality that’s packaged in the Cocoa frameworks can only be deliveredthrough object-oriented techniques. This book will explain how the frameworkswork and why this is the case.Second, because Objective-C is an extension of standard ANSI C, existing Cprograms can be adapted to use the software frameworks without losing any of thework that went into their original development. Since Objective-C incorporates C,you get all the benefits of C when working within Objective-C. You can choosewhen to do something in an object-oriented way (define a new class, for example)and when to stick to procedural programming techniques (define a structure andsome functions instead of a class).Moreover, Objective-C is a simple language. Its syntax is small, unambiguous, andeasy to learn. Object-oriented programming, with its self-conscious terminologyand emphasis on abstract design, often presents a steep learning curve to newrecruits. A well-organized language like Objective-C can make becoming aproficient object-oriented programmer that much less difficult. The size of this bookis a testament to the simplicity of Objective-C. It’s not a big book.Compared to other object oriented languages based on C, Objective-C is verydynamic. The compiler preserves a great deal of information about the objectsthemselves for use at runtime. Decisions that otherwise might be made at compiletime can be postponed until the program is running. This gives Objective-Cprograms unusual flexibility and power. For example, Objective-C’s dynamismyields two big benefits that are hard to get with other nominally object-orientedlanguages:15 Apple Computer, Inc. February 2003

C H A P T E R1Introduction Objective-C supports an open style of dynamic binding, a style that canaccommodate a simple architecture for interactive user interfaces. Messages arenot necessarily constrained by either the class of the receiver or the methodselector, so a software framework can allow for user choices at runtime andpermit developers freedom of expression in their design. (Terminology like“dynamic binding,” “message,” “class,” “receiver,” and “selector” will beexplained in due course in this book.) Objective-C’s dynamism enables the construction of sophisticated developmenttools. An interface to the runtime system provides access to information aboutrunning applications, so it’s possible to develop tools that monitor, intervene,and reveal the underlying structure and activity of Objective-C applications.How This Book is OrganizedThis book is divided into four chapters and two appendixes. The chapters are: “Object-Oriented Programming” (page 19) discusses the rationale forobject-oriented programming languages and introduces much of theterminology. It develops the ideas behind object-oriented programmingtechniques. If you’re already familiar with object-oriented programming and areinterested only in Objective-C, you may want to skip this chapter and go directlyto “The Objective-C Language” (page 55). “The Objective-C Language” (page 55) describes the basic concepts and syntaxof Objective-C. It covers many of the same topics as “Object-OrientedProgramming” (page 19), but looks at them from the standpoint of theObjective-C language. It reintroduces the terminology of object-orientedprogramming, but in the context of Objective-C. “The Objective-C Runtime System” (page 127) looks at the NSObject class andhow Objective-C programs interact with the runtime system. In particular, itexamines the paradigms for managing object allocations, dynamically loadingnew classes at runtime, and forwarding messages to other objects.16 Apple Computer, Inc. February 2003

C H A P T E R1Introduction “Objective-C Runtime Functions and Data Structures” (page 165) describes thedata structures and functions of the Objective-C runtime support library. Yourprograms can use these interfaces to interact with the Objective-C runtimesystem. For example, you can add classes or methods, or obtain a list of all classdefinitions for loaded classes.The appendixes contain reference material that might be useful for understandingthe language. They are: “Objective-C Language Summary” (page 205) lists and briefly comments on allof the Objective-C extensions to the C language. “Grammar for the Objective-C Language” (page 215) presents, withoutcomment, a formal grammar of the Objective-C extensions to the C language.This reference manual is meant to be read as a companion to the referencemanual for C presented in The C Programming Language by Brian W. Kernighanand Dennis M. Ritchie, published by Prentice Hall.ConventionsWhere this book discusses functions, methods, and other programming elements, itmakes special use of computer voice and italic fonts. Computer voice denoteswords or characters that are to be taken literally (typed as they appear). Italicdenotes words that represent something else or can be varied. For example, thesyntax@interfaceClassName ( CategoryName )means that @interface and the two parentheses are required, but that you canchoose the class name and category name.Where example code is shown, ellipsis indicates the parts, often substantial parts,that have been omitted:- (void)encodeWithCoder:(NSCoder *)coder{[super encodeWithCoder:coder];. . .17 Apple Computer, Inc. February 2003

C H A P T E R1Introduction}The conventions used in the reference appendix are described in that appendix.18 Apple Computer, Inc. February 2003

C H A P T E R22Object-Oriented ProgrammingProgramming languages have traditionally divided the world into two parts—dataand operations on data. Data is static and immutable, except as the operations maychange it. The procedures and functions that operate on data have no lasting stateof their own; they’re useful only in their ability to affect data.This division is, of course, grounded in the way computers work, so it’s not one thatyou can easily ignore or push aside. Like the equally pervasive distinctions betweenmatter and energy and between nouns and verbs, it forms the background againstwhich we work. At some point, all programmers—even object-orientedprogrammers—must lay out the data structures that their programs will use anddefine the functions that will act on the data.With a procedural programming language like C, that’s about all there is to it. Thelanguage may offer various kinds of support for organizing data and functions, butit won’t divide the world any differently. Functions and data structures are the basicelements of design.Object-oriented programming doesn’t so much dispute this view of the world asrestructure it at a higher level. It groups operations and data into modular unitscalled objects and lets you combine objects into structured networks to form acomplete program. In an object-oriented programming language, objects and objectinteractions are the basic elements of design.Every object has both state (data) and behavior (operations on data). In that, they’renot much different from ordinary physical objects. It’s easy to see how a mechanicaldevice, such as a pocket watch or a piano, embodies both state and behavior. Butalmost anything that’s designed to do a job does too. Even simple things with nomoving parts such as an ordinary bottle combine state (how full the bottle is,whether or not it’s open, how warm its contents are) with behavior (the ability todispense its contents at various flow rates, to be opened or closed, to withstand highor low temperatures).19 Apple Computer, Inc. February 2003

C H A P T E R2Object-Oriented ProgrammingIt’s this resemblance to real things that gives objects much of their power andappeal. They can not only model components of real systems, but equally as wellfulfill assigned roles as components in software systems.Interface and ImplementationAs humans, we’re constantly faced with myriad facts and impressions that we mustmake sense of. To do so, we have to abstract underlying structure away fromsurface details and discover the fundamental relations at work. Abstractions revealcauses and effects, expose patterns and frameworks, and separate what’s importantfrom what’s not. They’re at the root of understanding.To invent programs, you need to be able to capture the same kinds of abstractionsand express them in the program design.It’s the job of a programming language to help you do this. The language shouldfacilitate the process of invention and design by letting you encode abstractions thatreveal the way things work. It should let you make your ideas concrete in the codeyou write. Surface details shouldn’t obscure the architecture of your program.All programming languages provide devices that help express abstractions. Inessence, these devices are ways of grouping implementation details, hiding them,and giving them, at least to some extent, a common interface—much as amechanical object separates its interface from its implementation.20 Apple Computer, Inc. February 2003

C H A P T E R2Object-Oriented ProgrammingFigure 2-1Interface and ng at such a unit from the inside, as the implementor, you’d be concerned withwhat it’s composed of and how it works. Looking at it from the outside, as the user,you’re concerned only with what it is and what it does. You can look past the detailsand think solely in terms of the role that the unit plays at a higher level.The principal units of abstraction in the C language are structures and functions.Both, in different ways, hide elements of the implementation: On the data side of the world, C structures group data elements into larger unitswhich can then be handled as single entities. While some code must delve insidethe structure and manipulate the fields separately, much of the program canregard it as a single thing—not as a collection of elements, but as what thoseelements taken together represent. One structure can include others, so acomplex arrangement of information can be built from simpler layers.In modern C, the fields of a structure live in their own name space—that is, theirnames won’t conflict with identically named data elements outside thestructure. Partitioning the program name space is essential for keepingimplementation details out of the interface. Imagine, for example, the enormoustask of assigning a different name to every piece of data in a large program andof making sure new names don’t conflict with old ones.21 Apple Computer, Inc. February 2003

C H A P T E R2Object-Oriented Programming On the procedural side of the world, functions encapsulate behaviors that can beused repeatedly without being reimplemented. Data elements local to afunction, like the fields within a structure, are protected within their own namespace. Functions can reference (call) other functions, so quite complex behaviorscan be built from smaller pieces.Functions are reusable. Once defined, they can be called any number of timeswithout again considering the implementation. The most generally usefulfunctions can be collected in libraries and reused in many different applications.All the user needs is the function interface, not the source code.However, unlike data elements, functions aren’t partitioned into separate namespaces. Each function must have a unique name. Although the function may bereusable, its name is not.C structures and functions are able to express significant abstractions, but theymaintain the distinction between data and operations on data. In a proceduralprogramming language, the highest units of abstraction still live on one side or theother of the data-versus-operations divide. The programs you design must alwaysreflect, at the highest level, the way the computer works.Object-oriented programming languages don’t lose any of the virtues of structuresand functions—they go a step further and add a unit capable of abstraction at ahigher level, a unit that hides the interaction between a function and its data.Suppose, for example, that you have a group of functions that all act on a particulardata structure. You want to make those functions easier to use by, as far as possible,taking the structure out of the interface. So you supply a few additional functions tomanage the data. All the work of manipulating the data structure—allocating it,initializing it, getting information from it, modifying values within it, keeping it upto date, and freeing it—is done through the functions. All the user does is call thefunctions and pass the structure to them.With these changes, the structure has become an opaque token that otherprogrammers never need to look inside. They can concentrate on what the functionsdo, not how the data is organized. You’ve taken the first step toward creating anobject.The next step is to give this idea support in the programming language andcompletely hide the data structure so that it doesn’t even have to be passed betweenthe functions. The data becomes an internal implementation detail; all that’sexported to users is a functional interface. Because objects completely encapsulatetheir data (hide it), users can think of them solely in terms of their behavior.22 Apple Computer, Inc. February 2003

C H A P T E R2Object-Oriented ProgrammingWith this step, the interface to the functions has become much simpler. Callers don’tneed to know how they’re implemented (what data they use). It’s fair now to callthis an “object.”The hidden data structure unites all of the functions that share access to it. So, anobject is more than a collection of random functions; it’s a bundle of relatedbehaviors that are supported by shared data. To use a function that belongs to anobject, you first create the object (thus giving it its internal data structure), then tellthe object which function it should perform. You begin to think in terms of what theobject does, rather than in terms of the individual functions.This progression from thinking about functions and data structures to thinkingabout object behaviors is the essence of learning object-oriented programming. Itmay seem unfamiliar at first, but as you gain experience with object-orientedprogramming, you’ll find it’s a more natural way to think about things. Everydayprogramming terminology is replete with analogies to real-world objects of variouskinds—lists, containers, tables, controllers, even managers. Implementing suchthings as programming objects merely extends the analogy in a natural way.A programming language can be judged by the kinds of abstractions that it enablesyou to encode. You shouldn’t be distracted by extraneous matters or forced toexpress yourself using a vocabulary that doesn’t match the reality you’re trying tocapture.If, for example, you must always tend to the business of keeping the right datamatched with the right procedure, you’re forced at all times to be aware of the entireprogram at a low level of implementation. While you might still invent programs ata high level of abstraction, the path from imagination to implementation canbecome quite tenuous—and more and more difficult as programs become biggerand more complicated.By providing another, higher level of abstraction, object-oriented programminglanguages give you a larger vocabulary and a richer model to program in.23 Apple Computer, Inc. February 2003

C H A P T E R2Object-Oriented ProgrammingThe Object ModelThe insight of object-oriented programming is to combine state and behavior—dataand operations on data—in a high-level unit, an object, and to give it languagesupport. An object is a group of related functions and a data structure that servesthose functions. The functions are known as the object’s methods, and the fields ofits data structure are its instance variables. The methods wrap around the instancevariables and hide them from the rest of the program:Figure 2-2An Objectthoddatamem et h o dm et h o dm et h o dLikely, if you’ve ever tackled any kind of difficult programming problem, yourdesign has included groups of functions that work on a particular kind of data—implicit “objects” without the language support. Object-oriented programmingmakes these function groups explicit and permits you to think in terms of the group,rather than its components. The only way to an object’s data, the only interface, isthrough its methods.24 Apple Computer, Inc. February 2003

C H A P T E R2Object-Oriented ProgrammingBy

The Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming. Object-oriented programming, like most interesting developments, builds on some old ideas, extends them, and puts them together in novel ways. The result is many-faceted and a clear ste