OpenStep Development Tools
  Rechercher uniquement dans ce livre
Télécharger cet ouvrage au format PDF

The NSObject Class

7


CharacteristicDescription
Inherits From:none (NSObject is the root class.)
Conforms ToNSObject
Declared In:Foundation/NSObject.h
Foundation/NSRunLoop.h

Class Description

NSObject is the root class of all ordinary Objective C inheritance hierarchies; it has no superclass. Its interface derives from two sources: the methods it declares directly and those declared in the NSObject protocol. Its interface is divided in this way so that objects inheriting from other root classes (notably NSProxy) can stand in for ordinary objects without having to inherit from NSObject. The following discussion makes no distinction between the methods declared in this class and those declared in the NSObject protocol.
From NSObject, other classes inherit a basic interface to the runtime system for the Objective C language. It is through NSObject that instances of all classes obtain their ability to behave as objects. Among other things, the NSObject class provides inheriting classes with a framework for creating, initializing, deallocating, comparing, and archiving objects, for performing methods selected at run-time, for querying an object about its methods and its position in the inheritance hierarchy, and for forwarding messages to other
objects. For example, to ask an object what class it belongs to, you would send it a class message. To find out whether it implements a particular method, you would send it a respondsToSelector: message.
The NSObject class is an abstract class; programs use instances of classes that inherit from NSObject, but never of NSObject itself.

Initializing an Object to Its Class

Every object is connected to the runtime system through its isa instance variable, inherited from the NSObject class, which identifies the object's class; it points to a structure that is compiled from the class definition. Through isa, an object can find whatever information it needs at ru time, such as its place in the inheritance hierarchy, the size and structure of its instance variables, and the location of the method implementations it can perform in response to messages.
Because all ordinary objects inherit directly or indirectly from the NSObject class, they all have this variable. The defining characteristic of an "object" is that its first instance variable is an isa pointer to a class structure.
The installation of the class structure--the initialization of isa--is one of the responsibilities of the alloc and allocWithZone: methods, the same methods that create (allocate memory for) new instances of a class. In other words, class initialization is part of the process of creating an object; it is not left to the methods, such as init, that initialize individual objects with their particular characteristics.

Instance and Class Methods

Every object requires an interface to the runtime system, whether it is an instance object or a class object. For example, it should be possible to ask either an instance or a class whether it can respond to a particular message. So that this will not mean implementing every NSObject method twice, once as an instance method and again as a class method, the run-time system treats methods defined in the root class in a special way: Instance methods defined in the root class can be performed both by instances and by class objects.
A class object has access to class methods--those defined in the class and those inherited from the classes above it in the inheritance hierarchy--but generally not to instance methods. However, the runtime system gives all class objects
access to the instance methods defined in the root class. Any class object can perform any root instance method, provided it does not have a class method with the same name.
For example, a class object could be sent messages to perform NSObject's respondsToSelector: and perform:withObject: instance methods:
    SEL method = @selector(riskAll:);

    if ( [MyClass respondsToSelector:method] )
        [MyClass perform:method withObject:self];

When a class object receives a message, the run-time system looks first at the receiver's set of class methods. If it fails to find a class method that can respond to the message, it looks at the set of instance methods defined in the root class. If the root class has an instance method that can respond (as NSObject does for respondsToSelector: and perform:withObject:), the run-time system uses that implementation and the message succeeds.
Only instance methods available to a class object are those defined in the root class. If MyClass in the example above had reimplemented either respondsToSelector: or perform:withObject:, those new versions of the methods would be available only to instances. The class object for MyClass could perform only the versions defined in the NSObject class. (Of course, if MyClass had implemented respondsToSelector: or perform:withObject: as class methods rather than instance methods, the class would perform those new versions.)

Initializing the Class

MethodDescription
+ (void)initializeInitializes the class before it is used (before it receives its first message).

Creating and Destroying Instances

.
MethodDescription
+ (id)allocReturns a new, uninitialized instance of the receiving class
+ (id)allocWithZone:(NSZone *)zoneReturns a new, uninitialized instance of the receiving class in zone.
+ (id)newAllocates a new instance of the receiving class, sends it an init message, and returns the initialized object returned by init. This method is simply a convenient cover for the alloc and init methods.
- (id)copyInvokes copyWithZone:. This method is implemented in NSObject as a convenience to subclasses. A subclass need override only copyWithZone: for both copy and copyWithZone: to operate correctly.
- (void)deallocDeallocates the memory occupied by the receiver.
- (id)initImplemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.
- (id)mutableCopyInvokes mutableCopyWithZone:. This method is implemented in NSObject as a convenience to subclasses. A subclass need override only mutableCopyWithZone: for both mutableCopy: and mutableCopyWithZone: to operate correctly.

Identifying Classes

MethodDescription
+ (Class)classReturns self. Since this is a class method, it returns the class object.
+ (Class)superclassReturns the class object for the receiver's superclass.

Testing Class Functionality

MethodDescription
+ (BOOL)instancesRespondToSelector:(SEL)aSelectorReturns YES if instances of the class are capable of responding to aSelector messages, and NO if they are not.

Testing Protocol Conformance

MethodDescription
+ (BOOL)conformsToProtocol:(Protocol *)aProtocolReturns YES if the receiving class conforms to aProtocol, and NO if it does not.

Obtaining Method Information

MethodDescription
+ (IMP)instanceMethodForSelector:(SEL)aSelectorLocates and returns the address of the implementation of the aSelector instance method.
- (IMP)methodForSelector:(SEL)aSelectorLocates and returns the address of the receiver's implementation of the aSelector method, so that it can be called as a function.
- (NSMethodSignature *)
methodSignatureForSelector:(SEL)aSelector
Returns an object that contains
a description of the
aSelector method, or nil if
the aSelector method can
not be found.

Describing Objects

MethodDescription
+ (NSString *)descriptionSubclasses override this method to return a human-readable string representation of the contents of the receiver. NSObject's implementation simply prints the name of the receiver's class.

Posing

MethodDescription
+ (void)poseAsClass:(Class)aClassObjectCauses the receiving class to "pose as" its superclass.

Error Handling

MethodDescription
- (void)doesNotRecognizeSelector:(SEL)aSelectorHandles aSelector messages that the receiver does not recognize.

Sending Deferred Messages

MethodDescription
+(void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector

object:(id)anObject

Cancels previous perform requests having the same target and argument (as determined by isEqual:), and the same selector. This method removes timers only in the current run loop, not all run loops.
-(void)performSelector:(SEL)aSelector
object:(id)anObject
afterDelay:(NSTimeInterval)delay
Sends an aSelector
message to
anObject after
delay. self and
anObject are
retained until after
the action is
executed.

Forwarding Messages

MethodDescription
- (void)forwardInvocation:(NSInvocation *)anInvocationImplemented by subclasses to forward messages to other objects.

Archiving

MethodDescription
- (id)awakeAfterUsingCoder:
(NSCoder *)aDecoder
Implemented by subclasses to reinitialize
the receiver after unarchiving. The
NSObject implementation of this method
simply returns self.
- (Class)classForArchiverIdentifies the class to be used during archiving. NSObject's implementation returns the object returned by classForCoder:.
- (Class)classForCoderIdentifies the class to be used during serialization. An NSObject returns its own class by default.
- (id)replacementObjectForArchiver:
(NSArchiver *)anArchiver
Allows an object to substitute another object
for itself during archiving. NSObject's
implementation returns the object returned
by replacementObjectForCoder:.
- (id)replacementObjectForCoder:
(NSCoder *)anEncoder
Allows an object to substitute another object
for itself during serialization. NSObject's
implementation returns self.
+ (void)setVersion:(int)versionSets the class version number to version.
+ (int)versionReturns the version of the class definition.