欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS runtime forwardInvocation详解及整理

时间:2020-10-13 10:08:03|栏目:iOS代码|点击:

 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 *h1 = [[TestModelHelper1 alloc] init]; 
    TestModelHelper2 *h2 = [[TestModelHelper2 alloc] init]; 
    [anInvocation invokeWithTarget:h1]; 
    [anInvocation invokeWithTarget:h2]; 
  } 
} 

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中的target action控件的实现

栏    目:iOS代码

下一篇:iOS开发中如何实现一个平滑的颜色过渡

本文标题:iOS runtime forwardInvocation详解及整理

本文地址:http://www.codeinn.net/misctech/10730.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有