預設的performSelector支援最多傳遞兩個引數,要想傳遞超過兩個的引數,需要使用NSInvocation來模擬performSelector的行為,如下:
- (id)performSelector:(SEL)aSelector withObjects:(NSArray *)argumentsArray
{
NSMethodSignature *sig = [selfmethodSignatureForSelector:aSelector];
if (sig)
{
NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:sig];
[invocation setTarget:self];
[invocation setSelector:aSelector];
for (int i = 0; i < argumentsArray.count ; ++i)
{
id argument = [argumentsArray objectAtIndex:i];
//此處+2不能漏
[invocation setArgument:&argument atIndex:i + 2];
}
[invocation invoke];
if (sig.methodReturnLength)
{
id anObject = nil;
[invocation getReturnValue:anObject];
return anObject;
}
return nil;
}
returnnil;
}