NSMethodSignature顧名思義應(yīng)該就是“方法簽名”革答,類似于C++中的編譯器時的函數(shù)簽名榄融。蘋果官方定義該類為對方法的參數(shù)缕贡、返回類似進行封裝某饰,協(xié)同NSInvocation實現(xiàn)消息轉(zhuǎn)發(fā)儒恋。通過消息轉(zhuǎn)發(fā)實現(xiàn)類似C++中的多重繼承。
iOS中的SEL黔漂,它的作用和C诫尽、C++中的函數(shù)指針很相似,通過performSelector:withObject:函數(shù)可以直接調(diào)用這個消息炬守。但是perform相關(guān)的這些函數(shù)牧嫉,有一個局限性,其參數(shù)數(shù)量不能超過2個减途,否則要做很麻煩的處理酣藻,與之相對,NSInvocation也是一種消息調(diào)用的方法鳍置,并且它的參數(shù)沒有限制辽剧。這兩種直接調(diào)用對象消息的方法,在IOS4.0之后税产,大多被block結(jié)構(gòu)所取代怕轿,只有在很老的兼容性系統(tǒng)中才會使用。
簡單用法如下:
- (void)test {
int a = 1;
int b = 2;
int c = 3;
SEL myMethod = @selector(myLog:param:parm:);
SEL myMethod2 = @selector(myLog);
// 創(chuàng)建一個函數(shù)簽名辟拷,這個簽名可以是任意的撞羽,但需要注意,簽名函數(shù)的參數(shù)數(shù)量要和調(diào)用的一致衫冻。
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:myMethod];
// 通過簽名初始化
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
// 2.FirstViewController *view = self;
// 2.[invocation setArgument:&view atIndex:0];
// 2.[invocation setArgument:&myMethod2 atIndex:1];
// 設(shè)置target
// 1.[invocation setTarget:self];
// 設(shè)置selector
[invocation setSelector:myMethod];
// 注意:1诀紊、這里設(shè)置參數(shù)的Index 需要從2開始,因為前兩個被selector和target占用隅俘。
[invocation setArgument:&a atIndex:2];
[invocation setArgument:&b atIndex:3];
[invocation setArgument:&c atIndex:4];
// [invocation retainArguments];
// 我們將c的值設(shè)置為返回值
[invocation setReturnValue:&c];
int d;
// 取這個返回值
[invocation getReturnValue:&d];
NSLog(@"d:%d", d);
NSUInteger argCount = [sig numberOfArguments];
NSLog(@"argCount:%ld", argCount);
for (NSInteger i=0; i<argCount; i++) {
NSLog(@"%s", [sig getArgumentTypeAtIndex:i]);
}
NSLog(@"returnType:%s ,returnLen:%ld", [sig methodReturnType], [sig methodReturnLength]);
NSLog(@"signature:%@" , sig);
// 消息調(diào)用
2.[invocation invokeWithTarget:self];
}
注意:代碼中用1.標識的為第一種用法邻奠,通過setTarget和setSelector來設(shè)置NSInvocation的參數(shù),而用2.標識的是另一種用法为居,通過setArgument atIndex:來設(shè)置參數(shù)惕澎。看個人的喜好颜骤。唧喉。。
- (int)myLog:(int)a param:(int)b parm:(int)c
{
NSLog(@"MyLog:%d,%d,%d", a, b, c);
return a+b+c;
}
- (void)myLog
{
NSLog(@"你好,South China University of Technology");
}
消息轉(zhuǎn)發(fā)中的使用:
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
if (!signature) {
signature = [target methodSignatureForSelector:selector];
}
return signature;
}
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
if (sel == @selector(foo)) {
class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "V@:");
return YES;
}
return [super resolveInstanceMethod:sel];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if (!signature) {
for (id target in self.allDelegates) {
if ((signature = [target methodSignatureForSelector:aSelector])) {
break;
}
}
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
for (id target in self.allDelegates) {
if ([target respondsToSelector:anInvocation.selector]) {
[anInvocation invokeWithTarget:target];
}
}
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector]) {
return YES;
}
for (id target in self.allDelegates) {
if ([target respondsToSelector:aSelector]) {
return YES;
}
}
return NO;
}
備注: allDelegate是存儲delegate的數(shù)組結(jié)構(gòu)忍抽。