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

Objective-C之消息转发机制

作者: Mr杰杰 | 来源:发表于2018-07-17 12:26 被阅读5次
    1. 动态方法解析

    询问消息接收者的所属类,能否动态添加方法,以处理当前这个未知的选择子。

    void instanceMethod(id self, SEL _cmd)
    {
        NSLog(@"instanceMethod");
    }
    
    + (BOOL)resolveInstanceMethod:(SEL)sel
    {
        if (sel == @selector(instanceMethod)) {
            class_addMethod(self, sel, (IMP)instanceMethod, "V@:");
            /// 消息转发提前结束
            return YES;
        }
        return [super resolveInstanceMethod:sel];
    }
    

    除了实例方法以外,如果调用的是类方法,会调用另外一个方法:

    + (BOOL)resolveClassMethod:(SEL)sel;
    
    1. 备援接收者

    如果消息接收者的所属类无法通过动态添加方法处理这个未知的选择子,会寻找备援接收者。

    当前接受者如果不能处理这条消息,运行期系统会请求当前接受者让其他接受者处理这条消息,与之对应的方法是:

    - (id)forwardingTargetForSelector:(SEL)selector;
    

    一旦+resolveInstanceMethod:方法不能动态添加实例方法,该方法会返回NO下一步会调用-forwardingTargetForSelector:方法。

    - (id)forwardingTargetForSelector:(SEL)aSelector
    {
        People *people = [[People alloc] init];
        if ([people respondsToSelector:aSelector]) {
            /// 消息转发提前结束
            return people;
        }
        return [super forwardingTargetForSelector:aSelector];
    }
    
    1. 完整的消息转发机制

    如果找不到备援接收者,或者备援接收者无法响应这个选择子,就会触发完整的消息转发机制。

    完整的消息转发机制:首先创建NSInvocation对象,把尚未处理的那条消息有关的全部细节封装于这个对象中。此对象中包含选择子(selector)、目标(target)及参数(parameters)。在触发NSInvocation对象时,消息派发系统(message-dispatch system)将亲自触发,把消息派发给目标对象。与之对应的方法是:

    // 返回选择子的方法签名
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
    
    - (void)forwardInvocation:(NSInvocation *)anInvocation;
    

    methodSignatureForSelector:方法返回一个包含未知选择子的签名方法对象NSMethodSignature。

    如果methodSignatureForSelector:无法返回选择子的方法签名,就不会执行下一步的forwardInvocation:方法。

    forwardInvocation:方法是最后一次处理这条消息的机会,该方法没有返回值。

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
    {
        if (aSelector == @selector(instanceMethod)) {
            return [NSMethodSignature signatureWithObjCTypes:"V@:@"];
        }
        return [super methodSignatureForSelector:aSelector];
    }
    
    - (void)forwardInvocation:(NSInvocation *)anInvocation
    {
        if ([anInvocation selector] == @selector(instanceMethod)) {
            People *people = [[People alloc] init];
            [anInvocation invokeWithTarget:people];
        }
    }
    

    如果没有重写forwardInvocation:方法,系统就会调用该类的doesNotRecognizeSelector:方法通知不能识别该选择子。

    相关文章

      网友评论

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

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