iOS中runtime forwardInvocation的示例分析
更新:HHH   时间:2023-1-8


小编给大家分享一下iOS中runtime forwardInvocation的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

 iOS runtime forwardInvocation详解

代码:

TestModel

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { 
  if(aSelector == @selector(testMethod)) 
  { 
    return [NSMethodSignature signatureWithObjCTypes:"v@:"]; 
  } 
  return nil; 
} 
 
 
-(void)forwardInvocation:(NSInvocation *)anInvocation 
{ 
  if (anInvocation.selector == @selector(testMethod)) 
  { 
    TestModelHelper1 *h2 = [[TestModelHelper1 alloc] init]; 
    TestModelHelper2 *h3 = [[TestModelHelper2 alloc] init]; 
    [anInvocation invokeWithTarget:h2]; 
    [anInvocation invokeWithTarget:h3]; 
  } 
}

TestModelHelper1

-(void)testMethod 
{ 
  NSLog(@"i am TestModelHelper1"); 
} 
TestModelHelper2
[objc] view plain copy
-(void)testMethod 
{ 
  NSLog(@"i am TestModelHelper2"); 
}

主调用类

TestModel *model = [[TestModel alloc] init]; 
[model testMethod];

TestModel本身没有实现testMethod方法,但最终运行后,程序没有报错,且TestModelHelper1和TestModelHelper2的testMethod方法都被执行了

1.forwardingTargetForSelector同为消息转发,但在实践层面上有什么区别?何时可以考虑把消息下放到forwardInvocation阶段转发?

forwardingTargetForSelector仅支持一个对象的返回,也就是说消息只能被转发给一个对象

forwardInvocation可以将消息同时转发给任意多个对象

2.methodSignatureForSelector如何实现?

methodSignatureForSelector用于描述被转发的消息,描述的格式要遵循以下规则点击打开链接

首先,先要了解的是,每个方法都有self和_cmd两个默认的隐藏参数,self即接收消息的对象本身,_cmd即是selector选择器,所以,描述的大概格式是:返回值@:参数。@即为self,:对应_cmd(selector).返回值和参数根据不同函数定义做具体调整。

比如下面这个函数

-(void)testMethod;

返回值为void,没有参数,按照上面的表格中的符号说明,再结合上面提到的概念,这个函数的描述即为   v@:

v代表void,@代表self(self就是个对象,所以用@),:代表_cmd(selector)

再练一个

-(NSString *)testMethod2:(NSString *)str;

描述为 @@:@

第一个@代表返回值NSString*,对象;第二个@代表self;:代表_cmd(selector);第三个@代表参数str,NSString对象类型

如果实在拿不准,不会写,还可以简单写段代码,借助method_getTypeEncoding方法去查看某个函数的描述,比如

-(void)testMethod 
{ 
  NSLog(@"i am TestModelHelper1"); 
  Method method = class_getInstanceMethod(self.class, @selector(testMethod)); 
  const char *des = method_getTypeEncoding(method); 
  NSString *desStr = [NSString stringWithCString:des encoding:NSUTF8StringEncoding]; 
  NSLog(@"%@",desStr); 
}


把数字去掉,剩下v@:,与之前我们的描述一致

-(NSString *)testMethod2:(NSString *)str 
{ 
  Method method = class_getInstanceMethod(self.class, @selector(testMethod2:)); 
  const charchar *des = method_getTypeEncoding(method); 
  NSString *desStr = [NSString stringWithCString:des encoding:NSUTF8StringEncoding]; 
  NSLog(@"%@",desStr); 
  return @""; 
}

结果是@@:@,与之前结论一致

以上是“iOS中runtime forwardInvocation的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!

返回移动开发教程...