O''''Reilly Network For Information About''''s Book part 115 doc

6 118 0
O''''Reilly Network For Information About''''s Book part 115 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

Sets the isa pointer of the receiver to classObj, and returns the previous value of the pointer. This effectively converts the receiver to an instance of the class represented by classObj. This will only work if the receiver's class is a descendant of the class represented by classObj, and instances of the two classes have the same size. If the call fails, it returns nil. 1.10.3.8 Enforcing intentions Each method in the following list generates a runtime error. In each case, when you call the method, pass in the _cmd variable, which the runtime sets up to be the selector of the method you are in. -(id )subclassResponsibility:(SEL )sel Call this in a method that you require subclasses to override and implement. -(id )notImplemented:(SEL )sel Call this in a method that you have deliberately left unfinished. -(id )shouldNotImplement:(SEL )sel Call this in a method that you inherit (and override) but that makes no sense for your subclass to support. 1.10.3.9 Error handling The following functions are used for error handling. See Section 1.8 for more information about their usage. -(id )doesNotRecognize:(SEL )sel Generates a runtime error by invoking the -error: method on self. The runtime sends this message to an object when the object has failed to respond to, or forward, a message. -(id )error:(const char *)format , Writes an error message to the system log (via the syslog( ) call) and exits the program. The runtime calls this method on self for a variety of error conditions. Override as described in Section 1.8 to customize error handling for a class. 1.10.3.10 Archiving The GNU runtime provides the type TypedStream for reading and writing objects to and from archives. Section 1.13 describes this type and its use in saving and restoring objects. The following methods support archiving: -(id)awake Does nothing and returns self. Called by the runtime when an object has been reconstructed. Override to customize behavior when your objects are restored. -(id )write:(TypedStream*)stream Writes the receiver to the specified stream. -(id )read:(TypedStream*)stream Sets the receiver's fields by reading from the specified stream. +(id )setVersion:(int )version Sets the class version number. +(int)version Returns the class's version number, or zero if it has never been set. +(int )streamVersion:(TypedStream*)stream Returns the version number of the typed stream. 1.10.4 The NSObject Class NSObject supports most of the features of Object and adds others. Often the NSObject version of a method will have a more specifically typed return value. For example, -class is declared to return a Class and not just an id. In defining NSObject, Cocoa first declares the NSObject protocol. (It can be confusing that the class and protocol have the same name; they live in different namespaces.) This allows for other root classes. As long as they implement the NSObject protocol, they can be used in contexts that depend on the methods that NSObject implements. The NSObject class implements the NSObject protocol along with other useful methods that its descendants may want but that are not part of the protocol and are not important for interchangeability. Cocoa also declares categories for NSObject, such as NSComparisonMethods, NSURLClient, and others, comprising dozens of additional methods. This handbook discusses a few of these categories (NSCoding, NSKeyValueCoding) separately, but omits them from this section. In the following sections, the methods are grouped by topic, and marked as being part of the NSObject protocol or class. 1.10.4.1 Creating, copying, and freeing objects Section 1.7 discusses how to use the following methods, which create objects, copy objects, and free the memory used by objects: +(id)alloc Allocates, clears, and returns a pointer to memory for a new instance of the receiver. Memory comes from the default zone. Returns nil on failure. (NSObject class) +(id )allocWithZone:(NSZone*)zone Same as alloc, but uses a specific region of memory. NSZone is not a class but a C type that you should treat as opaque. You can get the zone of an object with the -zone method. You may improve cache performance by creating related objects in the same zone. Passing in a nil parameter means memory will be allocated from the default zone. (NSObject class) +(void)initialize Sent by the runtime before the class or any of its descendants is sent a message by the program. Each class gets the message exactly once, and superclasses get the message before subclasses. The NSObject implementation does nothing. Override (first passing the call to super) to perform per-class setup. (NSObject class) -(id)init Returns self. This is the designated initializer for NSObject. The Section 1.7 discusses overriding this method. (NSObject class) +(id)new Calls +alloc, then -init on the resulting instance, and returns the result. (NSObject class) +(id )copyWithZone:(NSZone*)zone Returns the receiver since class objects are unique. Ignores the parameter. Don't override. (NSObject class) +(id )mutableCopyWithZone:(NSZone*)zone Returns the receiver since class objects are unique. Ignores the parameter. Don't override. (NSObject class) -(id)copy Calls -copyWithZone: with a nil zone parameter. NSObject doesn't implement that method (only the class method of the same name) but many Cocoa classes do. Your class will have to implement -copyWithZone: in order to use -copy. The Section 1.7 discusses how to do this. -(id)mutableCopy Calls -mutableCopyWithZone: with a nil zone parameter. NSObject doesn't implement that method (only the class method of the same name) but many Cocoa classes do. Your class will have to implement - mutableCopyWithZone in order to use -mutableCopy. The Section 1.7 discusses how to do this. (NSObject class) -(void)dealloc Deallocates the memory used to store the receiver. Override to release any additional resources managed by your object. Don't call -dealloc directly if you are using retain/release memory management. (See Section 1.12.) (NSObject class) +(void)load Called by the runtime when a class or category is set up for use by the runtime. Deprecated in favor of the +initialize method. (NSObject class) 1.10.4.2 Identifying objects and classes Use the following methods to find out about an object's identity, and to compare it with other objects: -(Class)class Returns the class object for the receiver's class. (NSObject protocol) -(Class)superclass Returns the class object of the receiver's parent class. (NSObject protocol) -(NSString*)description Returns a string describing the state of the receiver. The NSObject implementation prints the class name and the runtime address of the instance. Override to provide a string the runtime will use when you print an object using the %@ formatting descriptor with NSLog( ). (NSObject protocol) -(id)self Returns the receiver. You can get a class object from the class's name with this method: Class circleClass = [Circle self]; (NSObject protocol) . override) but that makes no sense for your subclass to support. 1.10.3.9 Error handling The following functions are used for error handling. See Section 1.8 for more information about their usage or forward, a message. -(id )error:(const char *)format , Writes an error message to the system log (via the syslog( ) call) and exits the program. The runtime calls this method on self for. that its descendants may want but that are not part of the protocol and are not important for interchangeability. Cocoa also declares categories for NSObject, such as NSComparisonMethods, NSURLClient,

Ngày đăng: 07/07/2014, 08:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan