Lately I wrote an application in java (for android) which used reflection to invoke methods of some objects. The argument number and type was unknown, meaning, I had a unifi
I modified @JustSid answer and added more validation, nil argument support, changed it to an Obj-C NSObject category method, and add -performSelectorIfAvailable:
helper methods for easier use. Please enjoy! :)
#import
@implementation NSObject (performSelectorIfAvailable)
// Invokes a selector with an arbitrary number of arguments.
// Non responding selector or too few arguments will make this method do nothing.
// You can pass [NSNull null] objects for nil arguments.
- (void)invokeSelector:(SEL)selector arguments:(NSArray*)arguments {
if (![self respondsToSelector:selector]) return; // selector not found
// From -numberOfArguments doc,
// "There are always at least 2 arguments, because an NSMethodSignature object includes the hidden arguments self and _cmd, which are the first two arguments passed to every method implementation."
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
int numSelArgs = [signature numberOfArguments] - 2;
if (numSelArgs > [arguments count]) return; // not enough arguments in the array
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:selector];
for(int i=0; i < numSelArgs; i++) {
id arg = [arguments objectAtIndex:i];
if (![arg isKindOfClass:[NSNull class]]) {
[invocation setArgument:&arg atIndex:i + 2];
}
}
[invocation invoke]; // Invoke the selector
}