iOS中runtime forwardInvocation的示例分析

发布时间:2021-07-08 18:16:43 作者:小新
来源:亿速云 阅读:115

小编给大家分享一下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用于描述被转发的消息,描述的格式要遵循以下规则点击打开链接

iOS中runtime forwardInvocation的示例分析

首先,先要了解的是,每个方法都有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); 
}

iOS中runtime forwardInvocation的示例分析

把数字去掉,剩下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的示例分析

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

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

推荐阅读:
  1. iOS开发之Runtime常用示例总结
  2. 怎么在iOS中实现一个模拟定位功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios runtime

上一篇:iOS如何实现仿微信摇一摇动画效果加震动音效

下一篇:iOS中如何实现archive保存图片到本地

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》