Instance Method

responds(to:)

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

Required.

Declaration

func responds(to aSelector: Selector!) -> Bool

Parameters

aSelector

A selector that identifies a message.

Return Value

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

Discussion

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

You cannot test whether an object inherits a method from its superclass by sending responds(to:) 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 responds(to:) to super is equivalent to sending it to self. Instead, you must invoke the NSObject class method instancesRespond(to:) 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 false.

See Also

Testing Object Inheritance, Behavior, and Conformance

func isKind(of: AnyClass) -> Bool

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.

func isMember(of: AnyClass) -> Bool

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

Required.

func conforms(to: Protocol) -> Bool

Returns a Boolean value that indicates whether the receiver conforms to a given protocol.

Required.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software