Adopted by: NSObject
Declared in:
- Foundation/NSObject.h
The NSObject protocol groups methods that are fundamental to all Objective-C objects. If an object conforms to this protocol, it can be considered a first-class object. Such an object can be asked about its:
In addition, objects that conform to this protocol-with its retain, release, and autorelease methods-can also integrate with the object-management and deallocation scheme defined in the Foundation Kit. (See the introduction to the Foundation Kit for more information.) Thus, an object that conforms to the NSObject protocol can be managed by container objects like those defined by NSArray and NSDictionary.
The root class, NSObject, adopts this protocol, so virtually all objects have the features described by this protocol.
- Identifying classes
- - class
- - superclass
- Identifying and comparing objects
- - isEqual:
- - hash
- - self
- Determining allocation zones
- - zone
- Managing reference counts
- - retain
- - release
- - autorelease
- - retainCount
- Testing class functionality
- - respondsToSelector:
- Testing inheritance relationships
- - isKindOfClass:
- - isMemberOfClass:
- Testing protocol conformance
- - conformsToProtocol:
- Describing objects
- - description
- Sending messages
- - performSelector:
- - performSelector:withObject:
- - performSelector:withObject:withObject:
- Identifying proxies
- - isProxy
- (id)autorelease
self
. You
add an object to an autorelease pool so it will receive a release message-and
thus might be deallocated-when the pool is destroyed. For more
information on the autorelease mechanism, see the NSAutoreleasePool
class specification.
See Also: - retain, - retainCount
- (Class)class
See Also: + class (NSObject class)
- (BOOL)conformsToProtocol:(Protocol
*)aProtocol
See Also: + conformsToProtocol: (NSObject class)
- (NSString *)description
- (unsigned)hash
If a mutable object is added to a collection that uses hash values to determine the object's position in the collection, the value returned by the hash method of the object must not change while the object is in the collection. To accomplish this, either the hash method must not rely on any of the object's internal state information or you must make sure the object's internal state information does not change while the object is in the collection. (Note that it can be difficult to know whether or not a given object is in a collection.)
- (BOOL)isEqual:(id)anObject
- (BOOL)isKindOfClass:(Class)aClass
NSMutableData *myData = [NSMutableData dataWithCapacity:30]; id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData]; if ( [anArchiver isKindOfClass:[NSCoder class]] ) ...
Because instance methods defined in the root class can be sent to class objects, when the receiver is a class object, this method returns YES if aClass is NSObject, NO otherwise.
See Also: - isMemberOfClass:
- (BOOL)isMemberOfClass:(Class)aClass
NSMutableData *myData = [NSMutableData dataWithCapacity:30]; id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData]; if ([anArchiver isMemberOfClass:[NSCoder class]]) ...
When the receiver is a class object, this method returns NO. Class objects are not "members of" any class.
See Also: - isKindOfClass:
- (BOOL)isProxy
- (id)performSelector:(SEL)aSelector
performSelector: is equivalent to sending an aSelector message directly to the receiver. For example, all three of the following messages do the same thing:
id myClone = [anObject copy]; id myClone = [anObject performSelector:@selector(copy)]; id myClone = [anObject performSelector:sel_getUid("copy")];
However, the performSelector: method allows you to send messages that aren't determined until run time. A variable selector can be passed as the argument:
SEL myMethod = findTheAppropriateSelectorForTheCurrentSituation(); [anObject performSelector:myMethod];
aSelector should identify a method that takes no arguments. For methods that return anything other than an object, use NSInvocation.
See Also: - performSelector:withObject:, performSelector:withObject:withObject:
- (id)performSelector:(SEL)aSelector
withObject:(id)anObject
This method is the same as performSelector: except that you can supply an argument for aSelector. aSelector should identify a method that takes a single argument of type id. For methods with other argument types and return values, use NSInvocation.
See Also: - performSelector:withObject:withObject:, - methodForSelector: (NSObject class)
- (id)performSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject
See Also: - performSelector:withObject:, - methodForSelector: (NSObject class)
- (oneway void)release
You send release messages only to objects that you "own." By definition, you own objects that you create using one of the alloc... or copy... methods. These methods return objects with an implicit reference count of one. You also own (or perhaps share ownership in) an object that you send a retain message to because retain increments the object's reference count. Each retain message you send an object should be balanced eventually with a release or autorelease message, so the object can be deallocated. For more information on the automatic deallocation mechanism, see the introduction to the Foundation Kit.
You would only implement this method to define your own reference-counting
scheme. Such implementations should not invoke the inherited method;
that is, they should not include a release message to super
.
See Also: - retainCount
- (BOOL)respondsToSelector:(SEL)aSelector
Note that if the receiver is able to forward aSelector messages to another object, it will be able to respond to the message, albeit indirectly, even though this method returns NO.
See Also: - forwardInvocation: (NSObject class), + instancesRespondToSelector: (NSObject class)
- (id)retain
An object is deallocated automatically when its reference count reaches 0. retain messages increment the reference count, and release messages decrement it. For more information on this mechanism, see the introduction to the Foundation Kit.
As a convenience, retain returns self because it is often used in nested expressions:
NSString *systemApps = [[NSString stringWithCString:"/System/Applications"] retain];
You would only implement this method if you were defining
your own reference-counting scheme. Such implementations must return self and
should not invoke the inherited method by sending a retain message
to super
.
See Also: - autorelease, - release, - retainCount
- (unsigned)retainCount
UINT_MAX
,
as defined in <limits.h>
.
retainCount does not account for any pending autorelease messages sent to the receiver.
See Also: - autorelease, - retain
- (id)self
See Also: - class
- (Class)superclass
See Also: + superclass (NSObject class)
- (NSZone *)zone
See Also: + allocWithZone: (NSObject class)