NSMethodSignature和NSInvocation的一些用法

NSMethodSignature顾名思义应该就是“方法签名”,类似于C++中的编译器时的函数签名。苹果官方定义该类为对方法的参数、返回类似进行封装,协同NSInvocation实现消息转发。通过消息转发实现类似C++中的多重继承。

iOS中的SEL,它的作用和C、C++中的函数指针很相似,通过performSelector:withObject:函数可以直接调用这个消息。但是perform相关的这些函数,有一个局限性,其参数数量不能超过2个,否则要做很麻烦的处理,与之相对,NSInvocation也是一种消息调用的方法,并且它的参数没有限制。这两种直接调用对象消息的方法,在IOS4.0之后,大多被block结构所取代,只有在很老的兼容性系统中才会使用。
简单用法如下:
- (void)test {
int a = 1;
int b = 2;
int c = 3;
SEL myMethod = @selector(myLog:param:parm:);
SEL myMethod2 = @selector(myLog);
// 创建一个函数签名,这个签名可以是任意的,但需要注意,签名函数的参数数量要和调用的一致。
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];
// 设置target
// 1.[invocation setTarget:self];
// 设置selector
[invocation setSelector:myMethod];
// 注意:1、这里设置参数的Index 需要从2开始,因为前两个被selector和target占用。
[invocation setArgument:&a atIndex:2];
[invocation setArgument:&b atIndex:3];
[invocation setArgument:&c atIndex:4];
// [invocation retainArguments];
// 我们将c的值设置为返回值
[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

消息转发中的使用:
- (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的数组结构。

你可能感兴趣的:(NSMethodSignature和NSInvocation的一些用法)