实际上,无论如何我都找不到IMP
使用可变数量的参数进行调用,但是对于这种情况我有另一种解决方案。
通过使用NSInvocation
,我们可以解决这个问题。
void myFunc(id _self, SEL _cmdS, id first, ...) {
Class clazz = object_getClass(_self);
Class superClass = class_getSuperclass(clazz);
NSMethodSignature *signature = [superClass methodSignatureForSelector:_cmdS];
if (!signature) {
return;
}
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:signature];
[inv setSelector:_cmdS];
[inv setTarget:superClass];
va_list args;
va_start(args, first);
id arg = first;
// Arguments 0 and 1 are self and _cmd respectively, automatically set
// by NSInvocation. So start setting arguments from index 2
for (int i = 2; i < signature.numberOfArguments; i++) {
[inv setArgument:&arg atIndex:i];
if (i < signature.numberOfArguments - 1) {
arg = va_arg(args, id);
}
}
va_end(args);
[inv invoke];
}