when to use respondsToSelector in objective-c

Objective CIos

Objective C Problem Overview

- (void)someMethod
    if ( [delegate respondsToSelector:@selector(operationShouldProceed)] )
        if ( [delegate operationShouldProceed] )
            // do something appropriate

The documentation says:

> The precaution is necessary only for optional methods in a formal protocol or methods of an informal protocol

What does it mean? If I use a formal protocol I can just use [delegate myMethod]?

Objective C Solutions

Solution 1 - Objective C

You use it pretty much just when you think you need to: to check to see if an object implements the method you are about to call. Usually this is done when you have an optional methods or an informal protocol.

I've only ever used respondsToSelector when I'm writing code that must communicate with a delegate object.

if ([self.delegate respondsToSelector:@selector(engineDidStartRunning:)]) {
        [self.delegate engineDidStartRunning:self];

You sometimes would want to use respondsToSelector on any method that returns and id or generic NSObject where you aren't sure what the class of the returned object is.

Solution 2 - Objective C

Just to add to what @kubi said, another time I use it is when a method was added to a pre-existing class in a newer version of the frameworks, but I still need to be backwards-compatible. For example:

if ([myObject respondsToSelector:@selector(doAwesomeNewThing)]) {
  [myObject doAwesomeNewThing];
} else {
  [self doOldWorkaroundHackWithObject:myObject];

Solution 3 - Objective C

As kubi mentioned respondsToSelector is normally used when you have a an instance of a method that conforms to a protocol.

// Extend from the NSObject protocol so it is safe to call `respondsToSelector` 
@protocol MyProtocol <NSObject> 

// @required by default
- (void) requiredMethod;


- (void)optionalMethod;


Given and instance of this protocol we can safely call any required method.

id <MyProtocol> myObject = ... 
[myObject requiredMethod];

However, optional methods may or may not be implemented, so you need to check at runtime.

if ([myObject respondsToSelector:@selector(optionalMethod)]) 
     [myObject optionalMethod];

Doing this will prevent a crash with an unrecognised selector.

Also, the reason why you should declare protocols as an extension of NSObjects, i.e.

@protocol MyProtocol <NSObject> 

Is because the NSObject protocol declares the respondsToSelector: selector. Otherwise XCode would think that it is unsafe to call it.

Solution 4 - Objective C

Old question, but I have learned to be very cautios with using stuff like addTarget:@selector(fu:) because the method name is not checked nor included in refactoring by XCODE. This has caused me quite some trouble already. So now I made it a habbit to always embed stuff like addTarget or addObserver in a respondsToSelector-Check like so:

if([self respondsToSelector:@selector(buttonClicked:)]){
    [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    DebugLog(@"Warning - a class or delegate did not respond to selector in class %@", self);

I know its not super elegant, but i'd rather add some boilerplate code than have an unexpected crash of my apps in the App Store.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTahoView Question on Stackoverflow
Solution 1 - Objective CkubiView Answer on Stackoverflow
Solution 2 - Objective CDave DeLongView Answer on Stackoverflow
Solution 3 - Objective CRobertView Answer on Stackoverflow
Solution 4 - Objective CWirsingView Answer on Stackoverflow