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<argCount; i++) {
       NSLog(@"%s", [sig getArgumentTypeAtIndex:i]);
   }
   NSLog(@"returnType:%s ,returnLen:%ld", [sig methodReturnType], [sig methodReturnLength]);
   NSLog(@"signature:%@" , sig);

   // 消息调用
   2.[invocation invokeWithTarget:self];
}

注意:代码中用1.标识的为第一种用法,通过setTarget和setSelector来设置NSInvocation的参数,而用2.标识的是另一种用法,通过setArgument atIndex:来设置参数。看个人的喜好。。。

- (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");
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,824评论 0 9
  • 哈哈哈哈,看见大家都在写自己的学习总结,本猿在踌躇了三年后终于下定决心也写一些东西。 简单直接,直入主题将是宗旨!...
    大鹏鸟阅读 601评论 4 0
  • 这篇读书笔记主要介绍了Objective-C底层的一些东西,比如Objective-C对象模型、objc_msgS...
    C9090阅读 872评论 0 2
  • 1.在开发的时候可以创建一个工具类,继承自我们的AFN中的请求管理者,再控制器中真正发请求的代码使用自己封装的工具...
    红楼那一场梦阅读 3,542评论 2 3
  • UIWebView 什么是UIWebView UIWebView是iOS内置的浏览器控件 系统自带的Safari浏...
    Skywang阅读 736评论 2 6