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"?
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"?
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).
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);
}
[[someInstance class] instanceMethodSignatureForSelector:selector]
should just be [someInstance methodSignatureForSelector:selector]
@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
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.