4

let's say I have a selector that may be assigned to several different methods - each one has a different return value.

Is there a way to check what is the return value of the method the selector is holding before calling "performSelector"?

2
  • Can you give some sample code?
    – waldrumpus
    Jan 30, 2013 at 11:35
  • Do you mean the actual value, or the type? I don't think you can know the return value as it depends on the method execution. Jan 30, 2013 at 11:36

4 Answers 4

9

Is there a way to check what is the return value of the method the selector is holding before calling "performSelector"?

Value? No. Type? Yap. It seems that you want the return type of the method (or your question wouldn't make sense).

Method m = class_getInstanceMethod([SomeClass class], @selector(foo:bar:));
char type[128];
method_getReturnType(m, type, sizeof(type));

Then you can examine the returned type string in type. For example, "v" means void (google the full list).

2
  • Make sure to use: #import <objc/runtime.h> Feb 20, 2014 at 23:00
  • No need to use runtime functions. You can just get the method signature from the object and selector, and get the return type from there.
    – newacct
    Mar 24, 2014 at 8:32
7

you might use NSInvocation which is recommended in Apple Docs for this purpose

here is some sample code for using NSInvocation

    SEL selector = NSSelectorFromString(@"someSelector");
if ([someInstance respondsToSelector:selector]) {
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
                                [[someInstance class] instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:someInstance];
    [invocation invoke];
    float returnValue;
    [invocation getReturnValue:&returnValue];
    NSLog(@"Returned %f", returnValue);
}
2
  • [[someInstance class] instanceMethodSignatureForSelector:selector] should just be [someInstance methodSignatureForSelector:selector]
    – newacct
    Mar 24, 2014 at 8:33
  • Keep in mind to separate instance method and class method
    – ManuQiao
    Jun 13, 2018 at 0:51
1

If the method doesn’t return an object (it returns a primitive type), then use NSInvocation instead.

@implementation NSObject(SafePerformSelector)
-(id) performSelectorSafely:(SEL)aSelector;
{
    NSParameterAssert(aSelector != NULL);
    NSParameterAssert([self respondsToSelector:aSelector]);

    NSMethodSignature* methodSig = [self methodSignatureForSelector:aSelector];
    if(methodSig == nil)
        return nil;

    const char* retType = [methodSig methodReturnType];
    if(strcmp(retType, @encode(id)) == 0 || strcmp(retType, @encode(void)) == 0){
        return [self performSelector:aSelector];
    } else {
        NSLog(@"-[%@ performSelector:@selector(%@)] shouldn't be used. The selector doesn't return an object or void", [self class], NSStringFromSelector(aSelector));
        return nil;
    }
}
@end
-1

performSelector: always returns an id. The actual type returned is determined by the method you call; so there is no way to know it beforehand.

1
  • 1
    The answers above clearly explain that it IS possible to know it beforehand.
    – Legoless
    Jun 5, 2015 at 12:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.