我不明白为什么我们需要在NSInvocation
对象上调用setSelector
方法,当这些信息已经通过invocationWithMethodSignature
传递。
要创建NSInvocation
对象,我们执行以下操作:
SEL someSelector;
NSMethodSignature *signature;
NSInvocation *invocation;
someSelector = @selector(sayHelloWithString:);
//Here we use the selector to create the signature
signature = [SomeObject instanceMethodSignatureForSelector:someSelector];
invocation = [NSInvocation invocationWithMethodSignature:signature];
//Here, we again set the same selector
[invocation setSelector:someSelector];
[invocation setTarget:someObjectInstance];
[invocation setArgument:@"Loving C" atIndex:2];
请注意,我们将选择器传递给了[SomeObject instanceMethodSignatureForSelector: someSelector];
并再次传递给[invocation setSelector:someSelector];
。
我有什么遗漏的吗?
signature = [SomeObject instanceMethodSignatureForSelector:someSelector];
应该改为signature = [[SomeObject class] instanceMethodSignatureForSelector:someSelector]; 或者 signature = [SomeObject methodSignatureForSelector:someSelector];
。 - Brynjar