Objective-C Language and Gnustep Base Library Programming Manual
Total Page:16
File Type:pdf, Size:1020Kb
Objective-C Language and GNUstep Base Library Programming Manual Francis Botto (Brainstorm) Richard Frith-Macdonald (Brainstorm) Nicola Pero (Brainstorm) Adrian Robert Copyright c 2001-2004 Free Software Foundation Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another lan- guage, under the above conditions for modified versions. i Table of Contents 1 Introduction::::::::::::::::::::::::::::::::::::: 3 1.1 What is Object-Oriented Programming? :::::::::::::::::::::::: 3 1.1.1 Some Basic OO Terminology :::::::::::::::::::::::::::::: 3 1.2 What is Objective-C? :::::::::::::::::::::::::::::::::::::::::: 5 1.3 History :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 5 1.4 What is GNUstep? ::::::::::::::::::::::::::::::::::::::::::::: 6 1.4.1 GNUstep Base Library :::::::::::::::::::::::::::::::::::: 6 1.4.2 GNUstep Make Utility :::::::::::::::::::::::::::::::::::: 7 1.4.3 A Word on the Graphical Environment :::::::::::::::::::: 7 1.4.4 The GNUstep Directory Layout ::::::::::::::::::::::::::: 7 1.5 Building Your First Objective-C Program :::::::::::::::::::::: 8 2 The Objective-C Language ::::::::::::::::::: 11 2.1 Non OO Additions :::::::::::::::::::::::::::::::::::::::::::: 11 2.2 Objects ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 2.2.1 Id and nil :::::::::::::::::::::::::::::::::::::::::::::::: 12 2.2.2 Messages ::::::::::::::::::::::::::::::::::::::::::::::::: 12 2.2.3 Polymorphism:::::::::::::::::::::::::::::::::::::::::::: 13 2.3 Classes:::::::::::::::::::::::::::::::::::::::::::::::::::::::: 13 2.3.1 Inheritance::::::::::::::::::::::::::::::::::::::::::::::: 13 2.3.2 Inheritance of Methods ::::::::::::::::::::::::::::::::::: 14 2.3.3 Overriding Methods :::::::::::::::::::::::::::::::::::::: 14 2.3.4 Abstract Classes ::::::::::::::::::::::::::::::::::::::::: 14 2.3.5 Class Clusters :::::::::::::::::::::::::::::::::::::::::::: 15 2.4 NSObject: The Root Class :::::::::::::::::::::::::::::::::::: 15 2.4.1 The NSObject Protocol :::::::::::::::::::::::::::::::::: 16 2.5 Static Typing ::::::::::::::::::::::::::::::::::::::::::::::::: 16 2.5.1 Type Introspection ::::::::::::::::::::::::::::::::::::::: 16 2.5.2 Referring to Instance Variables ::::::::::::::::::::::::::: 17 2.6 Working with Class Objects ::::::::::::::::::::::::::::::::::: 17 2.6.1 Locating Classes Dynamically :::::::::::::::::::::::::::: 18 2.7 Naming Constraints and Conventions:::::::::::::::::::::::::: 18 2.8 Strings in GNUstep ::::::::::::::::::::::::::::::::::::::::::: 19 2.8.1 Creating NSString Static Instances ::::::::::::::::::::::: 19 2.8.2 NSString +stringWithFormat: :::::::::::::::::::::::::::: 19 2.8.3 C String Conversion :::::::::::::::::::::::::::::::::::::: 20 2.8.4 NSMutableString::::::::::::::::::::::::::::::::::::::::: 20 2.8.5 Loading and Saving Strings::::::::::::::::::::::::::::::: 21 ii Objective-C GNUstep Base Programming Manual 3 Working with Objects::::::::::::::::::::::::: 23 3.1 Initializing and Allocating Objects :::::::::::::::::::::::::::: 23 3.1.1 Initialization with Arguments::::::::::::::::::::::::::::: 23 3.1.2 Memory Allocation and Zones :::::::::::::::::::::::::::: 24 3.1.3 Memory Deallocation::::::::::::::::::::::::::::::::::::: 25 3.2 Memory Management ::::::::::::::::::::::::::::::::::::::::: 25 3.2.1 Explicit Memory Management :::::::::::::::::::::::::::: 25 3.2.2 OpenStep-Style (Retain/Release) Memory Management::: 26 3.2.2.1 Autorelease Pools ::::::::::::::::::::::::::::::::::: 27 3.2.2.2 Avoiding Retain Cycles:::::::::::::::::::::::::::::: 28 3.2.2.3 Summary ::::::::::::::::::::::::::::::::::::::::::: 29 3.2.3 Garbage Collection Based Memory Management :::::::::: 29 3.2.4 Current Recommendations ::::::::::::::::::::::::::::::: 30 4 Writing New Classes :::::::::::::::::::::::::: 31 4.1 Interface :::::::::::::::::::::::::::::::::::::::::::::::::::::: 31 4.1.1 Interface Capabilities ::::::::::::::::::::::::::::::::::::: 31 4.1.2 Including Interfaces :::::::::::::::::::::::::::::::::::::: 33 4.1.3 Referring to Classes - @class :::::::::::::::::::::::::::::: 33 4.2 Implementation ::::::::::::::::::::::::::::::::::::::::::::::: 34 4.2.1 Writing an Implementation ::::::::::::::::::::::::::::::: 35 4.2.2 Super and Self ::::::::::::::::::::::::::::::::::::::::::: 35 4.2.3 Instance Initialization :::::::::::::::::::::::::::::::::::: 36 4.2.4 Flexible Initialization::::::::::::::::::::::::::::::::::::: 39 4.2.5 Instance Deallocation :::::::::::::::::::::::::::::::::::: 40 4.3 Protocols ::::::::::::::::::::::::::::::::::::::::::::::::::::: 41 4.3.1 Declaring a Formal Protocol :::::::::::::::::::::::::::::: 41 4.3.2 Implementing a Formal Protocol:::::::::::::::::::::::::: 42 4.3.3 Using a Formal Protocol ::::::::::::::::::::::::::::::::: 42 4.4 Categories :::::::::::::::::::::::::::::::::::::::::::::::::::: 43 4.4.1 Category Overrides::::::::::::::::::::::::::::::::::::::: 44 4.4.2 Categories as an Implementation Tool :::::::::::::::::::: 44 4.4.3 Categories and Protocols ::::::::::::::::::::::::::::::::: 45 4.5 Simulating Private and Protected Methods :::::::::::::::::::: 45 4.6 Simulating Class Variables :::::::::::::::::::::::::::::::::::: 46 5 Advanced Messaging :::::::::::::::::::::::::: 49 5.1 How Messaging Works :::::::::::::::::::::::::::::::::::::::: 49 5.2 Selectors :::::::::::::::::::::::::::::::::::::::::::::::::::::: 50 5.2.1 The Target-Action Paradigm ::::::::::::::::::::::::::::: 50 5.2.2 Obtaining Selectors :::::::::::::::::::::::::::::::::::::: 51 5.2.3 Avoiding Messaging Errors when an Implementation is Not Found :::::::::::::::::::::::::::::::::::::::::::::::::::::: 51 5.3 Forwarding ::::::::::::::::::::::::::::::::::::::::::::::::::: 52 5.4 Implementations :::::::::::::::::::::::::::::::::::::::::::::: 53 iii 6 Exception Handling, Logging, and Assertions :::::::::::::::::::::::::::::::::::::::::::::::: 55 6.1 Exceptions :::::::::::::::::::::::::::::::::::::::::::::::::::: 55 6.1.1 Catching and Handling Exceptions ::::::::::::::::::::::: 55 6.1.2 Passing Exceptions Up the Call Stack :::::::::::::::::::: 57 6.1.3 Where do Exceptions Originate?:::::::::::::::::::::::::: 57 6.1.4 Creating Exceptions :::::::::::::::::::::::::::::::::::::: 57 6.1.5 When to Use Exceptions ::::::::::::::::::::::::::::::::: 58 6.2 Logging ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 59 6.2.1 NSLog ::::::::::::::::::::::::::::::::::::::::::::::::::: 59 6.2.2 NSDebugLog, NSWarnLog ::::::::::::::::::::::::::::::: 59 6.2.3 Last Resorts: GSPrintf and fprintf:::::::::::::::::::::::: 61 6.2.4 Profiling Facilities :::::::::::::::::::::::::::::::::::::::: 61 6.3 Assertions :::::::::::::::::::::::::::::::::::::::::::::::::::: 61 6.3.1 Assertions and their Handling :::::::::::::::::::::::::::: 61 6.3.2 Custom Assertion Handling :::::::::::::::::::::::::::::: 62 6.4 Comparison with Java :::::::::::::::::::::::::::::::::::::::: 62 7 Distributed Objects ::::::::::::::::::::::::::: 65 7.1 Object Interaction :::::::::::::::::::::::::::::::::::::::::::: 65 7.2 The GNUstep Solution :::::::::::::::::::::::::::::::::::::::: 65 7.2.1 Code at the Server ::::::::::::::::::::::::::::::::::::::: 66 7.2.2 Code at the Client ::::::::::::::::::::::::::::::::::::::: 67 7.2.3 Using a Protocol ::::::::::::::::::::::::::::::::::::::::: 69 7.2.4 Complete Code for Telephone Directory Application :::::: 70 7.2.5 GNUstep Distributed Objects Name Server ::::::::::::::: 71 7.2.6 Look Ma, No Stubs! :::::::::::::::::::::::::::::::::::::: 72 7.3 A More Involved Example::::::::::::::::::::::::::::::::::::: 72 7.3.1 Protocol Adopted at Client ::::::::::::::::::::::::::::::: 73 7.3.2 Protocol Adopted at Server::::::::::::::::::::::::::::::: 73 7.3.3 Code at the Client ::::::::::::::::::::::::::::::::::::::: 73 7.3.4 Code at the Server ::::::::::::::::::::::::::::::::::::::: 76 7.4 Language Support for Distributed Objects::::::::::::::::::::: 81 7.4.1 Protocol Type Qualifiers ::::::::::::::::::::::::::::::::: 81 7.4.2 Message Forwarding :::::::::::::::::::::::::::::::::::::: 83 7.5 Error Checking ::::::::::::::::::::::::::::::::::::::::::::::: 84 7.5.1 Vending the Server Object:::::::::::::::::::::::::::::::: 84 7.5.2 Catching Exceptions ::::::::::::::::::::::::::::::::::::: 84 7.5.3 The Connection Fails::::::::::::::::::::::::::::::::::::: 84 iv Objective-C GNUstep Base Programming Manual 8 Base Library ::::::::::::::::::::::::::::::::::: 85 8.1 Copying, Comparing, Hashing Objects::::::::::::::::::::::::: 85 8.2 Object Containers::::::::::::::::::::::::::::::::::::::::::::: 86 8.3 Data and Number Containers ::::::::::::::::::::::::::::::::: 87 8.3.1 NSData :::::::::::::::::::::::::::::::::::::::::::::::::: 87 8.3.2 NSValue ::::::::::::::::::::::::::::::::::::::::::::::::: 88 8.3.3 NSNumber ::::::::::::::::::::::::::::::::::::::::::::::: 88 8.3.4 NSRange, NSPoint, NSSize, NSRect :::::::::::::::::::::: 89 8.4 Date/Time Facilities :::::::::::::::::::::::::::::::::::::::::: 89 8.5 String Manipulation and Text Processing :::::::::::::::::::::: 89 8.5.1 NSScanner and Character Sets ::::::::::::::::::::::::::: 89 8.5.2 Attributed Strings:::::::::::::::::::::::::::::::::::::::: 89 8.5.3 Formatters :::::::::::::::::::::::::::::::::::::::::::::::