iOS Developer Library — Pre-Release

Developer

Foundation Framework Reference NSObject Protocol Reference

Options
Deployment Target:

On This Page
Language:

NSObject

Inheritance


Not Applicable

Conforms To


Not Applicable

Import Statement


Swift

import ObjectiveC

Not Applicable

Availability


Available in iOS 2.0 and later.

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:

  • Class, and the place of its class in the inheritance hierarchy

  • Conformance to protocols

  • Ability to respond to a particular message

The Cocoa root class, NSObject, adopts this protocol, so all objects inheriting from NSObject have the features described by this protocol.

  • class - class Required

    Returns the class object for the receiver’s class. (required)

    Declaration

    Objective-C

    - (Class)class

    Return Value

    The class object for the receiver’s class.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

    See Also

    class (NSObject class)

  • Returns the class object for the receiver’s superclass. (required)

    Declaration

    Swift

    var superclass: AnyClass? { get }

    Objective-C

    @property(readonly) Class superclass

    Return Value

    The class object for the receiver’s superclass.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

    See Also

    superclass (NSObject class)

  • Returns a Boolean value that indicates whether the receiver and a given object are equal. (required)

    Declaration

    Swift

    func isEqual(_ anObject: AnyObject?) -> Bool

    Objective-C

    - (BOOL)isEqual:(id)anObject

    Parameters

    anObject

    The object to be compared to the receiver. May be nil, in which case this method returns NOfalse.

    Return Value

    YEStrue if the receiver and anObject are equal, otherwise NOfalse.

    Discussion

    This method defines what it means for instances to be equal. For example, a container object might define two containers as equal if their corresponding objects all respond YEStrue to an isEqual: request. See the NSData, NSDictionary, NSArray, and NSString class specifications for examples of the use of this method.

    If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

    See Also

    – hash

  • hash - hash Required

    Returns an integer that can be used as a table address in a hash table structure. (required)

    Declaration

    Swift

    var hash: Int { get }

    Objective-C

    @property(readonly) NSUInteger hash

    Return Value

    An integer that can be used as a table address in a hash table structure.

    Discussion

    If two objects are equal (as determined by the isEqual: method), they must have the same hash value. This last point is particularly important if you define hash in a subclass and intend to put instances of that subclass into a collection.

    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. Therefore, 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. Thus, for example, a mutable dictionary can be put in a hash table but you must not change it while it is in there. (Note that it can be difficult to know whether or not a given object is in a collection.)

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

    See Also

    – isEqual:

  • self() - self Required

    Returns the receiver. (required)

    Declaration

    Swift

    func `self`() -> Self

    Objective-C

    - (instancetype)self

    Return Value

    The receiver.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

    See Also

    – class

  • Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. (required)

    Declaration

    Swift

    func isKindOfClass(_ aClass: AnyClass) -> Bool

    Objective-C

    - (BOOL)isKindOfClass:(Class)aClass

    Parameters

    aClass

    A class object representing the Objective-C class to be tested.

    Return Value

    YEStrue if the receiver is an instance of aClass or an instance of any class that inherits from aClass, otherwise NOfalse.

    Discussion

    For example, in this code, isKindOfClass: would return YEStrue because, in Foundation, the NSArchiver class inherits from NSCoder:

    • NSMutableData *myData = [NSMutableData dataWithCapacity:30];
    • id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];
    • if ( [anArchiver isKindOfClass:[NSCoder class]] )
    • ...

    Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:

    • // DO NOT DO THIS!
    • if ([myArray isKindOfClass:[NSMutableArray class]])
    • {
    • // Modify the object
    • }

    If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.

    If the receiver is a class object, this method returns YEStrue if aClass is a Class object of the same type, NOfalse otherwise.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a Boolean value that indicates whether the receiver is an instance of a given class. (required)

    Declaration

    Swift

    func isMemberOfClass(_ aClass: AnyClass) -> Bool

    Objective-C

    - (BOOL)isMemberOfClass:(Class)aClass

    Parameters

    aClass

    A class object representing the Objective-C class to be tested.

    Return Value

    YEStrue if the receiver is an instance of aClass, otherwise NOfalse.

    Discussion

    For example, in this code, isMemberOfClass: would return NOfalse:

    • NSMutableData *myData = [NSMutableData dataWithCapacity:30];
    • id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];
    • if ([anArchiver isMemberOfClass:[NSCoder class]])
    • ...

    Class objects may be compiler-created objects but they still support the concept of membership. Thus, you can use this method to verify that the receiver is a specific Class object.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message. (required)

    Declaration

    Swift

    func respondsToSelector(_ aSelector: Selector) -> Bool

    Objective-C

    - (BOOL)respondsToSelector:(SEL)aSelector

    Parameters

    aSelector

    A selector that identifies a message.

    Return Value

    YEStrue if the receiver implements or inherits a method that can respond to aSelector, otherwise NOfalse.

    Discussion

    The application is responsible for determining whether a NOfalse response should be considered an error.

    You cannot test whether an object inherits a method from its superclass by sending respondsToSelector: to the object using the super keyword. This method will still be testing the object as a whole, not just the superclass’s implementation. Therefore, sending respondsToSelector: to super is equivalent to sending it to self. Instead, you must invoke the NSObject class method instancesRespondToSelector: directly on the object’s superclass, as illustrated in the following code fragment.

    • if( [MySuperclass instancesRespondToSelector:@selector(aMethod)] ) {
    • // invoke the inherited method
    • [super aMethod];
    • }

    You cannot simply use [[self superclass] instancesRespondToSelector:@selector(aMethod)] since this may cause the method to fail if it is invoked by a subclass.

    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 NOfalse.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

    See Also

    forwardInvocation: (NSObject class)
    instancesRespondToSelector: (NSObject class)

  • Returns a Boolean value that indicates whether the receiver conforms to a given protocol. (required)

    Declaration

    Swift

    func conformsToProtocol(_ aProtocol: Protocol) -> Bool

    Objective-C

    - (BOOL)conformsToProtocol:(Protocol *)aProtocol

    Parameters

    aProtocol

    A protocol object that represents a particular protocol.

    Return Value

    YEStrue if the receiver conforms to aProtocol, otherwise NOfalse.

    Discussion

    This method works identically to the conformsToProtocol: class method declared in NSObject. It’s provided as a convenience so that you don’t need to get the class object to find out whether an instance can respond to a given set of messages.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a string that describes the contents of the receiver. (required)

    Declaration

    Swift

    var description: String { get }

    Objective-C

    @property(readonly, copy) NSString *description

    Return Value

    A string that describes the contents of the receiver.

    Discussion

    This method is used to create a textual representation of an object, for example in a formatted string:

    • ClassName *anObject = <#An object#>;
    • NSString *string = [NSString stringWithFormat:@"anObject is %@", anObject];

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a string that describes the contents of the receiver for presentation in the debugger.

    Declaration

    Swift

    optional var debugDescription: String { get }

    Objective-C

    @property(readonly, copy) NSString *debugDescription

    Return Value

    A string that describes the contents of the receiver for presentation in the debugger.

    Discussion

    The debugger’s print-object command invokes this method to produce a textual description of an object.

    NSObject implements this method by calling through to the description method. Thus, by default, an object’s debug description is the same as its description. However, you can override debugDescription if you want to decouple these.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 5.0 and later.

    See Also

    – description

  • Sends a specified message to the receiver and returns the result of the message. (required)

    Declaration

    Objective-C

    - (id)performSelector:(SEL)aSelector

    Parameters

    aSelector

    A selector identifying the message to send. If aSelector is NULL, an NSInvalidArgumentException is raised.

    Return Value

    An object that is the result of the message.

    Discussion

    The performSelector: method 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 runtime. A variable selector can be passed as the argument:

    • SEL myMethod = findTheAppropriateSelectorForTheCurrentSituation();
    • [anObject performSelector:myMethod];

    The aSelector argument should identify a method that takes no arguments. For methods that return anything other than an object, use NSInvocation.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Sends a message to the receiver with an object as the argument. (required)

    Declaration

    Objective-C

    - (id)performSelector:(SEL)aSelector withObject:(id)anObject

    Parameters

    aSelector

    A selector identifying the message to send. If aSelector is NULL, an NSInvalidArgumentException is raised.

    anObject

    An object that is the sole argument of the message.

    Return Value

    An object that is the result of the message.

    Discussion

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Sends a message to the receiver with two objects as arguments. (required)

    Declaration

    Objective-C

    - (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject

    Parameters

    aSelector

    A selector identifying the message to send. If aSelector is NULL, an NSInvalidArgumentException is raised.

    anObject

    An object that is the first argument of the message.

    anotherObject

    An object that is the second argument of the message

    Return Value

    An object that is the result of the message.

    Discussion

    This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • isProxy() - isProxy Required

    Returns a Boolean value that indicates whether the receiver does not descend from NSObject. (required)

    Declaration

    Swift

    func isProxy() -> Bool

    Objective-C

    - (BOOL)isProxy

    Return Value

    NOfalse if the receiver really descends from NSObject, otherwise YEStrue.

    Discussion

    This method is necessary because sending isKindOfClass: or isMemberOfClass: to an NSProxy object will test the object the proxy stands in for, not the proxy itself. Use this method to test if the receiver is a proxy (or a member of some other root class).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • retain - retain Required

    Increments the receiver’s reference count. (required)

    Declaration

    Objective-C

    - (instancetype)retain

    Return Value

    self.

    Discussion

    You send an object a retain message when you want to prevent it from being deallocated until you have finished using it.

    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 Advanced Memory Management Programming Guide.

    As a convenience, retain returns self because it may be used in nested expressions.

    You would implement this method only 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.

    Special Considerations

    Instead of using manual reference counting, you should adopt ARC—see Transitioning to ARC Release Notes.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • release - release Required

    Decrements the receiver’s reference count. (required)

    Declaration

    Objective-C

    - (oneway void)release

    Discussion

    The receiver is sent a dealloc message when its reference count reaches 0.

    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.

    For more information on the reference counting mechanism, see Advanced Memory Management Programming Guide.

    Special Considerations

    Instead of using manual reference counting, you should adopt ARC—see Transitioning to ARC Release Notes.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Decrements the receiver’s retain count at the end of the current autorelease pool block. (required)

    Declaration

    Objective-C

    - (instancetype)autorelease

    Return Value

    self.

    Discussion

    For more information about autorelease pool blocks, see Advanced Memory Management Programming Guide.

    Special Considerations

    Instead of using manual reference counting, you should adopt ARC—see Transitioning to ARC Release Notes.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Do not use this method. (required)

    Declaration

    Objective-C

    - (NSUInteger)retainCount

    Return Value

    The receiver’s reference count.

    Special Considerations

    This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

    To understand the fundamental rules of memory management that you must abide by, read Memory Management Policy. To diagnose memory management problems, use a suitable tool:

    • The Clang Static analyzer can typically find memory management problems even before you run your program.

    • The Object Alloc instrument in the Instruments application (see Instruments User Guide) can track object allocation and destruction.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • zone - zone Required

    Zones are deprecated and ignored by most classes that have it as a parameter. (required)

    Declaration

    Objective-C

    - (struct _NSZone *)zone

    Return Value

    A pointer to the zone from which the receiver was allocated.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.