您现在的位置是:首页 > 文章详情

iOS开发-消息转发

日期:2017-06-27点击:383

消息转发是OC运行时比较重要的特性,Objective-C运行时的主要的任务是负责消息分发,我们在开发中"unrecognized selector sent to instance xx",实例对象没有实现对应的消息,通常我们只需要实现未实现的方法即可。一般情况我们处理一个方法,运行时寻找匹配的selector然后执行,但是有时候只想在运行时才创建某个方法,消息确没有具体的实现,这个时候就会出出现运行时错误,按照消息转发的顺序我们有三种解决办法。

动态方法处理

首先我们来看一个简单的例子,定义一个Message类,定义一个responseMethod方法,不实现方法,直接调用:

1
2
Message  *msg=[[Message alloc]init];
[msg responseMethod];

错误信息如下:

1
*** Terminating app due to uncaught exception  'NSInvalidArgumentException' , reason:  '-[Message responseMethod]: unrecognized selector sent to instance 0x10011ad20'

动态方法的重写有两个可以调用方法:

1
2
+ (BOOL)resolveClassMethod:(SEL)sel __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
+ (BOOL)resolveInstanceMethod:(SEL)sel __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

此时我们只需要需要重写+resolveInstanceMethod:返回YES就可以解决错误信息,注意我们需要通过class_method方法添加新的函数取代原有的sel:

1
2
3
4
5
6
7
8
9
10
11
12
+(BOOL)resolveInstanceMethod:(SEL)sel{
     NSLog( @"FlyElephant-http://www.cnblogs.com/xiaofeixiang/" );
     if  (sel==@selector(responseMethod)) {
         class_addMethod([self  class ],sel, (IMP) dynamicMethodIMP,  "v@:" );
         return  YES;
     }
     return  [super resolveClassMethod:sel];
 
}
+(BOOL)resolveClassMethod:(SEL)sel{
     return  [super resolveClassMethod:sel];
}

动态执行的函数:

1
2
3
4
void  dynamicMethodIMP(id self, SEL _cmd)
{
     NSLog( @"Developer--dynamicMethodIMP--%@" ,NSStringFromSelector(_cmd));
}

消息转发

如果上面的方法没有重写或者说是返回NO,那么我们接下来的按照顺序还有两种选择,两种选择的原理都是一样,消息有对应的target,我们需要更换对应的target即可:

1
- (id)forwardingTargetForSelector:(SEL)aSelector __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
1
<span style= "font-family: 'Microsoft YaHei'; font-size: 14px;" >forwardingTargetForSelector返回参数是一个对象,如果对象非nil、非self,系统会将运行的消息转发给这个对象执行,否则会执行第三种解决方案。</span>
1
2
3
4
5
6
7
8
-(id)forwardingTargetForSelector:(SEL)aSelector{
     NSLog( @"FlyElephant-http://www.cnblogs.com/xiaofeixiang/" );
     NSLog( @"forwardingTargetForSelector" );
     if  (aSelector==@selector(responseMethod)) {
          return  developer;
     }
     return  self;
}

第二种消息是将消息发送到另外一个对象,如果想要修改消息,那么就要使用-forwardInvocation:,运行时将消息打包成NSInvocation,然后返回给你处理。处理完之后,调用invokeWithTarget,但是如果是是调用-forwardInvocation是无法执行成功,在执行之前我们进行方法签名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
     NSLog( @"methodSignatureForSelector" );
     if  ([super respondsToSelector:aSelector]) {
         return    [super methodSignatureForSelector:aSelector];
     } else {
         return   [developer methodSignatureForSelector:aSelector];
     }
}
 
 
-( void )forwardInvocation:(NSInvocation *)anInvocation{
       NSLog( @"forwardInvocation" );
     SEL  sel=[anInvocation selector];
     if  ([developer respondsToSelector:sel]) {
         [anInvocation invokeWithTarget:developer];
     } else {
         [super forwardInvocation:anInvocation];
     }
}

关于Developer类中的方法:

1
2
3
4
5
6
7
8
@implementation Developer
 
-( void )responseMethod{
     NSLog( @"博客园:FlyElephant-http://www.cnblogs.com/xiaofeixiang/" );
     NSLog( @"Developer----responseMethod" );
}
 
@end

Cocoa中代理(Proxies)和响应链(Responder Chain)用到了消息转发。NSProxy是一个轻量级的class,它的作用就是转发消息到另一个object。如果想要惰性加载object的某个属性会很有用。NSUndoManager也有用到,不过是截取消息,之后再执行,而不是转发到其他的地方。                                    

 响应链是关于Cocoa如何处理与发送事件与行为到对应的对象,通常我们处理的键盘文本框事件First Responder,如果没有处理该消息,则转发到下一个-nextResponder。这么一直下去直到找到能够处理该消息的object,或者没有找到,报错。

本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/5095023.html,如需转载请自行联系原作者


原文链接:https://yq.aliyun.com/articles/362449
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章