美文网首页
Objective-C 消息转发

Objective-C 消息转发

作者: Hunter琼 | 来源:发表于2018-03-29 10:53 被阅读22次

前文提到(https://www.jianshu.com/p/588d1d8c3ee1),消息动态方法解析时会重载+(BOOL)resolveInstanceMethod:+(BOOL)resolveClassMethod; 当这两个方法返回NO时,消息转发机制就会触发.
一 重定向(备援接受者)
在消息转发之前可以通过-(id)fowardingTargetForSelector:(SEL)aSelecto中把消息转给其它接受者来处理.
新建RedirectionMessageMethod类

@interface RedirectionMessageMethod : NSObject
//被其它接受者处理的方法
- (void)redirectionMethod:(int)temp;
#import "RedirectionMessageMethod.h"
#import "OtherHandelMethod.h"

@implementation RedirectionMessageMethod
- (id)forwardingTargetForSelector:(SEL)aSelector
{
    if ([NSStringFromSelector(aSelector) isEqualToString:@"redirectionMethod:"]) {
        // 把这个方法转给OtherHandelMethodl类
        OtherHandelMethod *otherTar  = [OtherHandelMethod new];
        return otherTar;
    }
    return [super forwardingTargetForSelector:aSelector];
    
}

新建OtherHandelMethod类

#import "OtherHandelMethod.h"

@implementation OtherHandelMethod

- (void)redirectionMethod:(int)temp
{
    NSLog(@"实现RedirectionMessageMethod过来的方法redirectionMethod:");
    
}

运行结果

2018-03-28 10:15:13.051529+0800 OC_Categroy[1015:57719] 实现RedirectionMessageMethod过来的方法redirectionMethod:

二 转发
当消息转发机制触发时,执行forwardInvocation:;而该方法的参数是NSInvocation类型的对象(封装了原始的消息和消息参数);通过实现该方法对不能处理的消息做一些默认的处理,也可以将消息转发给其它某个对象来处理.当我们重写forwardInvocation:方法时,同时也要重写methodSignatureForSelector:方法,否则抛异常!
forwardInvocation:方法就像一个不能识别的消息的分发中心,将这些消息转发给不同接收对象。或者它也可以象一个运输站将所有的消息都发送给同一个接收对象。它可以将一个消息翻译成另外一个消息,或者简单的”吃掉“某些消息,因此没有响应也没有错误。forwardInvocation:方法也可以对不同的消息提供同样的响应,这一切都取决于方法的具体实现。该方法所提供是将不同的对象链接到消息链的能力。
注意: forwardInvocation:方法只有在消息接收对象中无法正常响应消息时才会被调用。 所以,如果我们希望一个对象将negotiate消息转发给其它对象,则这个对象不能有negotiate方法。否则,forwardInvocation:将不可能会被调用。
将上文提到的消息重定向变成完全转发,需要在OtherHandelMethod创建NSMethodSignature对象

// 重写methodSignatureForSelector方法 进行完整转发
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    if ([NSStringFromSelector(aSelector) isEqualToString:@"redirectionMethod:"]) {
        NSMethodSignature *sign = [NSMethodSignature signatureWithObjCTypes:"v@:@"];
        return sign;
    }
    return [super methodSignatureForSelector:aSelector];
    
}

然后实现forwardInvocation:方法将消息分发给InvocationModel

// 消息完整转发给InvocationModel类  InvocationModel里面在实现charge:方法
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    InvocationModel *model = [InvocationModel new];
    anInvocation.selector  = NSSelectorFromString(@"charge:");
    if ([model respondsToSelector:[anInvocation selector]]) {
        [anInvocation invokeWithTarget:model];
    }else{
        [super forwardInvocation:anInvocation];
    }
}

新建InvocationModel类

#import "InvocationModel.h"

@implementation InvocationModel

- (void)charge:(int)chareInt
{
    NSLog(@"将redirectionMethod方法转发了");
    
}

运行结果:

2018-03-29 10:38:28.380568+0800 OC_Categroy[921:42779] 将redirectionMethod方法转发了

参考文献::http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/

相关文章

  • iOS理解Objective-C中消息转发机制附Demo

    iOS理解Objective-C中消息转发机制附Demo iOS理解Objective-C中消息转发机制附Demo

  • Objective-C 运行时(Runtime)解析

    Objective-C基于C语言加入了面向对象特性和消息转发,Objective-C 的消息转发需要运行时系统来动...

  • 面试题整理

    Objective-C 中的消息与消息转发 深入理解RunLoop Autorelease

  • 2018-02-01

    《Objective-C runtime系列 1》消息发送及转发机制 Objective-C是基于C,加入了面...

  • Objective-C 消息转发

    一些概念 静态绑定:在编译期就能决定运行时所应调用的函数。代表语言:C、C++等动态绑定:所要调用的函数直到运行期...

  • Objective-C 消息转发

    前文提到(https://www.jianshu.com/p/588d1d8c3ee1),消息动态方法解析时会重载...

  • Objective-C消息转发

    理论 1.消息机制 RunTime简称运行时。就是系统在运行的时候的一些机制,其中最主要的是消息机制。 对于C语言...

  • Objective-C消息转发

    OC中调用方法就是向对象发送消息下面的代码: 如果说method方法不存在,就会出现:unrecognized s...

  • Objective-C消息转发

    1. performSelector:object performSelector在运行时调用方法,由于编译期间不...

  • Objective-C消息转发

    我们要通过一个小例子来简单、通俗的理解一下什么是消息转发以及如何消息转发,希望看完这篇文章时大家会彻底的明白OC的...

网友评论

      本文标题:Objective-C 消息转发

      本文链接:https://www.haomeiwen.com/subject/itfycftx.html