ebook img

The Objective-C Programming Language - Andrew T. Duchowski PDF

240 Pages·2003·2.62 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Objective-C Programming Language - Andrew T. Duchowski

I n s i d e M a c O S X The Objective-C Programming Language February 2003  Apple Computer, Inc. Even though Apple has reviewed this © 2002 Apple Computer, Inc. manual, APPLE MAKES NO All rights reserved. WARRANTY OR REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH No part of this publication may be RESPECT TO THIS MANUAL, ITS reproduced, stored in a retrieval QUALITY, ACCURACY, system, or transmitted, in any form or MERCHANTABILITY, OR FITNESS by any means, mechanical, electronic, FOR A PARTICULAR PURPOSE. AS A photocopying, recording, or RESULT, THIS MANUAL IS SOLD “AS otherwise, without prior written IS,” AND YOU, THE PURCHASER, ARE permission of Apple Computer, Inc., ASSUMING THE ENTIRE RISK AS TO with the following exceptions: Any ITS QUALITY AND ACCURACY. person is hereby authorized to store documentation on a single computer IN NO EVENT WILL APPLE BE LIABLE for personal use only and to print FOR DIRECT, INDIRECT, SPECIAL, copies of documentation for personal INCIDENTAL, OR CONSEQUENTIAL use provided that the documentation DAMAGES RESULTING FROM ANY contains Apple’s copyright notice. DEFECT OR INACCURACY IN THIS The Apple logo is a trademark of MANUAL, even if advised of the Apple Computer, Inc. possibility of such damages. Use of the “keyboard” Apple logo THE WARRANTY AND REMEDIES SET (Option-Shift-K) for commercial FORTH ABOVE ARE EXCLUSIVE AND purposes without the prior written IN LIEU OF ALL OTHERS, ORAL OR consent of Apple may constitute WRITTEN, EXPRESS OR IMPLIED. No trademark infringement and unfair Apple dealer, agent, or employee is competition in violation of federal authorized to make any modification, and state laws. extension, or addition to this warranty. No licenses, express or implied, are Some states do not allow the exclusion or granted with respect to any of the limitation of implied warranties or technology described in this book. liability for incidental or consequential Apple retains all intellectual property damages, so the above limitation or rights associated with the technology exclusion may not apply to you. This described in this book. This book is warranty gives you specific legal rights, intended to assist application and you may also have other rights which developers to develop applications vary from state to state. only for Apple-labeled or Apple-licensed computers. Every effort has been made to ensure that the information in this document is accurate. Apple is not responsible for typographical errors. Apple Computer, Inc. 1 Infinite Loop Cupertino, CA 95014 408-996-1010 Apple, the Apple logo, and Macintosh are trademarks of Apple Computer, Inc., registered in the United States and other countries. HotSpot is a trademark of Sun Microsystems, Inc. Simultaneously published in the United States and Canada. Contents Figures, Listings, and Tables 11 Chapter 1 Introduction The Development Environment 14 Why Objective-C 15 How This Book is Organized 16 Conventions 17 Chapter 2 Object-Oriented Programming Interface and Implementation 20 The Object Model 24 The Messaging Metaphor 26 Classes 28 Modularity 29 Reusability 30 Mechanisms Of Abstraction 31 Encapsulation 32 Polymorphism 33 Inheritance 35 Class Hierarchies 35 Subclass Definitions 36 Uses of Inheritance 37 Dynamism 39 Dynamic Typing 39 Dynamic Binding 40 Dynamic Loading 43 Structuring Programs 44 Outlet Connections 45 3  Apple Computer, Inc. February 2003 C O N T E N T S Extrinsic and Intrinsic Connections 47 Activating the Object Network 47 Aggregation and Decomposition 48 Models and Frameworks 49 Structuring the Programming Task 50 Collaboration 51 Organizing Object-Oriented Projects 52 Designing on a Large Scale 52 Separating the Interface from the Implementation 52 Modularizing the Work 52 Keeping the Interface Simple 53 Making Decisions Dynamically 53 Inheriting Generic Code 53 Reusing Tested Code 54 Chapter 3 The Objective-C Language Objective-C Objects 55 id 56 Dynamic Typing 57 Object Messaging 58 The Receiver’s Instance Variables 59 Polymorphism 60 Dynamic Binding 60 Classes 62 Inheritance 62 The NSObject Class 64 Inheriting Instance Variables 64 Inheriting Methods 65 Overriding One Method With Another 66 Abstract Classes 66 Class Types 67 Static Typing 67 Type Introspection 68 Class Objects 68 Creating Instances 70 Customization With Class Objects 71 4  Apple Computer, Inc. February 2003 C O N T E N T S Variables and Class Objects 72 Initializing a Class Object 73 Methods of the Root Class 74 Class Names in Source Code 74 Defining a Class 75 The Interface 76 Importing the Interface 78 Referring to Other Classes 79 The Role of the Interface 79 The Implementation 80 Referring to Instance Variables 82 The Scope of Instance Variables 83 How Messaging Works 87 Selectors 90 Methods and Selectors 91 Method Return and Argument Types 91 Varying the Message at Runtime 92 The Target-Action Paradigm 92 Avoiding Messaging Errors 94 Hidden Arguments 94 Messages to self and super 95 An Example 96 Using super 99 Redefining self 99 Extending Classes 101 Categories—Adding Methods to Existing Classes 101 Adding to a Class 102 How Categories Are Used 103 Categories of the Root Class 103 Protocols—Declaring Interfaces for Others to Implement 104 When to Use Protocols 105 Enabling Static Behaviors 115 Static Typing 116 Type Checking 117 Return and Argument Types 118 Static Typing to an Inherited Class 118 Getting a Method Address 120 Getting an Object Data Structure 120 5  Apple Computer, Inc. February 2003 C O N T E N T S Using C++ With Objective-C 122 Mixing Objective-C and C++ Language Features 122 C++ Lexical Ambiguities and Conflicts 125 Chapter 4 The Objective-C Runtime System Memory Management 129 Allocating and Initializing Objects 129 The Returned Object 130 Arguments 131 Coordinating Classes 132 The Designated Initializer 134 Combining Allocation and Initialization 139 Retaining Objects 140 Handling Cyclical References 141 Deallocation 142 Releasing Shared Objects 143 Releasing Instance Variables 144 Marking Objects for Later Release 144 Object Ownership 145 Forwarding 146 Forwarding and Multiple Inheritance 149 Surrogate Objects 150 Forwarding and Inheritance 150 Dynamic Loading 152 Remote Messaging 152 Distributed Objects 153 Language Support 155 Synchronous and Asynchronous Messages 156 Pointer Arguments 157 Proxies and Copies 159 Type Encodings 160 Chapter 5 Objective-C Runtime Functions and Data Structures Objective-C Functions 165 6  Apple Computer, Inc. February 2003 C O N T E N T S Accessing Selectors 166 sel_getName 166 sel_isMapped 167 sel_registerName 167 sel_getUid 168 Sending Messages 168 objc_msgSend 169 objc_msgSend_stret 169 objc_msgSendSuper 170 objc_msgSendSuper_stret 171 Forwarding Messages 172 objc_msgSendv 172 objc_msgSendv_stret 173 marg_malloc 173 marg_free 174 marg_getRef 174 marg_getValue 174 marg_setValue 175 Adding Classes 175 objc_addClass 175 Accessing Methods 178 class_getInstanceMethod 178 class_getClassMethod 179 class_nextMethodList 179 class_addMethods 180 class_removeMethods 181 method_getNumberOfArguments 182 method_getSizeOfArguments 182 method_getArgumentInfo 182 Accessing Instance Variable Definitions 183 class_getInstanceVariable 183 Accessing the Class Version 183 class_setVersion 183 class_getVersion 184 Posing As Another Class 185 class_poseAs 185 Obtaining Class Definitions 185 objc_getClassList 186 7  Apple Computer, Inc. February 2003 C O N T E N T S objc_getClass 187 objc_lookUpClass 188 objc_getMetaClass 188 objc_setClassHandler 189 Instantiating Classes 189 class_createInstance 189 class_createInstanceFromZone 190 Accessing Instance Variables 190 object_setInstanceVariable 191 object_getInstanceVariable 191 Objective-C Callbacks 192 Class Handler Callback 192 Objective-C Data Types 192 Class Definition Data Structures 193 objc_class 194 objc_ivar 196 objc_ivar_list 197 objc_method 198 objc_method_list 199 objc_cache 200 objc_protocol_list 201 Instance Data Types 201 objc_object 202 objc_super 202 Appendix A Objective-C Language Summary Messages 205 Defined Types 206 Preprocessor Directives 207 Compiler Directives 207 Classes 208 Categories 209 Formal Protocols 210 Method Declarations 211 Method Implementations 212 Naming Conventions 212 8  Apple Computer, Inc. February 2003 C O N T E N T S Appendix B Grammar for the Objective-C Language External Declarations 217 Type Specifiers 221 Type Qualifiers 222 Primary Expressions 222 Chapter C Document Revision History Glossary 227 Index 233 9  Apple Computer, Inc. February 2003 C O N T E N T S 10  Apple Computer, Inc. February 2003

Description:
3 Apple Computer, Inc. February 2003 Contents Figures, Listings, and Tables 11 Chapter 1 Introduction The Development Environment 14 Why Objective-C 15
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.