源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS runtime forwardInvocation详解及整理

  • 时间:2022-01-15 19:32 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS runtime forwardInvocation详解及整理
[b] iOS runtime forwardInvocation详解[/b] 代码: 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用于描述被转发的消息,描述的格式要遵循以下规则[url=https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1]点击打开链接[/url] [img]http://files.jb51.net/file_images/article/201702/2017211145102015.png?2017111145254[/img] 首先,先要了解的是,每个方法都有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); 
} 
[img]http://files.jb51.net/file_images/article/201702/2017211145629214.png?2017111145653[/img] 把数字去掉,剩下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 @""; 
} 
[img]http://files.jb51.net/file_images/article/201702/2017211145732059.png?2017111145746[/img] 结果是@@:@,与之前结论一致 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部