Use NSInvocation
- (id)performSelector:(SEL)selector withParameters:(NSArray *)parameters
{
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:selector];
for (int i = 0; i < parameters.count; ++i)
{
id para = parameters[i];
[invocation setArgument:¶ atIndex:2+i];
}
[invocation invoke];
id ret;
[invocation getReturnValue:&ret];
return ret;
}
Note: this implementation only works if the invoked method takes ObjC objects as arguments and return an object. i.e. it won't work for something takes int or return double.
If you want it works for primitive types/struct, you have to check NSMethodSignature
for arguments type and convert object to that type then pass it to setArgument:atIndex:
.
Read this question for more detailed ansowers