博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用forwardInvocation实现消息重定向
阅读量:4327 次
发布时间:2019-06-06

本文共 3172 字,大约阅读时间需要 10 分钟。

 

在obj-c中我们可以向一个实例发送消息,相当于c/c++ java中的方法调用,只不过在这儿是说发送消息,实例收到消息后会进行一些处理。比如我们想调用一个方法,便向这个实例发送一个消息,实例收到消息后,如果能respondsToSelector,那么就会调用相应的方法。如果不能respond一般情况下会crash。今天要的,就是不让它crash。

 

首先说一下向一个实例发送一个消息后,系统是处理的流程:

1. 发送消息如:[self startwork] 

2. 系统会check是否能response这个消息

3. 如果能response则调用相应方法,不能则抛出异常

 

在第二步中,系统是如何check实例是否能response消息呢?如果实例本身就有相应的response,那么就会相应之,如果没有系统就会发出methodSignatureForSelector消息,寻问它这个消息是否有效?有效就返回对应的方法地址之类的,无效则返回nil。如果是nil就会crash, 如果不是nil接着发送forwardInvocation消息。

所以我们在重写methodSignatureForSelector的时候就人工让其返回有效实例。  文字说不清,还是用代码说明

我们定义了这样一个类

 

  1. @interface TargetProxy : NSProxy {  
  2.     id realObject1;  
  3.     id realObject2;  
  4. }  
  5.    
  6. - (id)initWithTarget1:(id)t1 target2:(id)t2;  
  7.    
  8. @end  
实现:

 

 

  1. @implementation TargetProxy  
  2.    
  3. - (id)initWithTarget1:(id)t1 target2:(id)t2 {  
  4.     realObject1 = [t1 retain];  
  5.     realObject2 = [t2 retain];  
  6.     return self;  
  7. }  
  8.    
  9. - (void)dealloc {  
  10.     [realObject1 release];  
  11.     [realObject2 release];  
  12.     [super dealloc];  
  13. }  
  14.    
  15. // The compiler knows the types at the call site but unfortunately doesn't  
  16. // leave them around for us to use, so we must poke around and find the types  
  17. // so that the invocation can be initialized from the stack frame.  
  18.    
  19. // Here, we ask the two real objects, realObject1 first, for their method  
  20. // signatures, since we'll be forwarding the message to one or the other  
  21. // of them in -forwardInvocation:.  If realObject1 returns a non-nil  
  22. // method signature, we use that, so in effect it has priority.  
  23. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {  
  24.     NSMethodSignature *sig;  
  25.     sig = [realObject1 methodSignatureForSelector:aSelector];  
  26.     if (sig) return sig;  
  27.     sig = [realObject2 methodSignatureForSelector:aSelector];  
  28.     return sig;  
  29. }  
  30.    
  31. // Invoke the invocation on whichever real object had a signature for it.  
  32. - (void)forwardInvocation:(NSInvocation *)invocation {  
  33.     id target = [realObject1 methodSignatureForSelector:[invocation selector]] ? realObject1 : realObject2;  
  34.     [invocation invokeWithTarget:target];  
  35. }  
  36.    
  37. // Override some of NSProxy's implementations to forward them...  
  38. - (BOOL)respondsToSelector:(SEL)aSelector {  
  39.     if ([realObject1 respondsToSelector:aSelector]) return YES;  
  40.     if ([realObject2 respondsToSelector:aSelector]) return YES;  
  41.     return NO;  
  42. }  
  43.    
  44. @end  
现在我们还用这个类,注意向它发送的消息:

 

 

  1. // Create a proxy to wrap the real objects.  This is rather  
  2.     // artificial for the purposes of this example -- you'd rarely  
  3.     // have a single proxy covering two objects.  But it is possible.  
  4.     id proxy = [[TargetProxy alloc] initWithTarget1:string target2:array];  
  5.    
  6.     // Note that we can't use appendFormat:, because vararg methods  
  7.     // cannot be forwarded!  
  8.     [proxy appendString:@"This "];  
  9.     [proxy appendString:@"is "];  
  10.     [proxy addObject:string];  
  11.     [proxy appendString:@"a "];  
  12.     [proxy appendString:@"test!"];  
  13.    
  14.     NSLog(@"count should be 1, it is: %d", [proxy count]);  
  15.       
  16.     if ([[proxy objectAtIndex:0] isEqualToString:@"This is a test!"]) {  
  17.         NSLog(@"Appending successful.");  
  18.     } else {  
  19.         NSLog(@"Appending failed, got: '%@'", proxy);  
  20.     }  
运行的结果是:

 

count should be 1, it is:  1

Appending successful.

TargetProxy声明中是没有appendString与addObject消息的,在这儿却可以正常发送,不crash,原因就是发送消息的时候,如果原本类没有这个消息响应的时候,转向询问methodSignatureForSelector,接着在forwardInvocation将消息重定向。 上面也说了多参数的消息是不能重定向的。不过貌似要实现这个要关掉ARC。

转载于:https://www.cnblogs.com/fuland/p/3675596.html

你可能感兴趣的文章
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
vue项目开发之v-for列表渲染的坑
查看>>
C# 输出流转化成输入流操作XML
查看>>
CSS外边距合并(塌陷/margin越界)
查看>>
Swift给每个开发者赢取500万的机会!不看一生后悔。
查看>>
UIView详解
查看>>
MSSQL如何将查询结果拼接成字符串
查看>>
20169217 《Linux内核原理与分析》 第十周作业
查看>>
20169217 2016-2017-2 《网络攻防实践》第四周学习总结
查看>>
MemCache在Windows下环境的搭建及启动
查看>>
<nginx.conf> nginx设置用户权限
查看>>
python实现redis三种cas事务操作
查看>>
同步异步与阻塞非阻塞
查看>>
C++ 安全单例模式总结
查看>>