iOS 消息转发

作者: Fsn_soul | 来源:发表于2016-04-09 10:17 被阅读268次

    通常给一个对象发送一条消息,如果这个对象没有能力处理这条消息,系统将会报错。然而,在报错之前,runtime system 会再给这个对象一个机会去处理这条消息。runtime会给这个对象发送一条forwardInvocation:消息,这条消息有一个唯一的参数:NSInvocation对象,只不过NSObject的forwardInvocation:方法实现默认调用doesNotRecognizeSelector:方法,而doesNotRecognizeSelector:方法只是简单的抛出一个异常.
    为了转发一条消息,forwardInvocation:方法需要做如下两点:

    1. 决定这条消息应该去哪里。
    2. 把这条带着它原本参数的消息发送到那里。
    - (void)forwardInvocation:(NSInvocation *)anInvocation
    {
        if ([someOtherObject respondsToSelector: [anInvocation selector]])
         {
             [anInvocation invokeWithTarget:someOtherObject]; //通过这个方法来发送原本的那条消息,someOtherObject是转发后接收该消息的对象。
         }
        else
         {
            [super forwardInvocation:anInvocation];
         }
    }
    

    forwardInvocation:方法担任着未识别消息分发中心的角色。你需要在这个方法里把未识别的消息转发给能够处理该消息的对象.
    注意:只有当某个对象不能响应某条消息时,这条消息才会到达forwardInvocation:。
    举例:你想让你的对象转发一条negotiate消息给另一个对象,那么它自己就不能有一个能响应negotiate消息的negotiate方法。如果它自己有这么一个方法,那么这条negotiate消息将永远也不会到达forwardInvocation:。

    虽然一个对象可以通过消息转发来到达“貌似”能处理之前不能处理的消息,
    但是通过- (BOOL)respondsToSelector:(SEL)aSelector方法,那么该方法依旧返回的是NO。如果你想让你的对象看起来更像是继承于另一个对象,比如让- (BOOL)respondsToSelector:(SEL)aSelector方法返回YES,那么你就必须,重写- (BOOL)respondsToSelector:(SEL)aSelector方法。

    - (BOOL)respondsToSelector:(SEL)aSelector
    {
        if ( [super respondsToSelector:aSelector] )
            return YES;
        else {
    
            /* Here, test whether the aSelector message can     *
    
             * be forwarded to another object and whether that  *
    
             * object can respond to it. Return YES if it can.  */
        }
    
        return NO;
    }
    

    类似需要重写的方法还有In addition to respondsToSelector: and isKindOfClass:, the instancesRespondToSelector: method should also mirror the forwarding algorithm. If protocols are used, the conformsToProtocol: method should likewise be added to the list。

    为了确保转发消息的准确性,需要重写methodSignatureForSelector:方法。

    - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
    {
        NSMethodSignature *signature = [super 
    methodSignatureForSelector:selector];
    
        if (!signature) {
           signature = [surrogate methodSignatureForSelector:selector];
        }
        return signature;
    }
    

    综上,消息转发,需要重写以上三个方法。其中methodSignatureForSelector:和forwardInvocation:是必须要重写的.执行顺序是先methodSignatureForSelector:再forwardInvocation:.只重写forwardInvocation:没用,仍然会崩溃.当一个对象不能处理某个方法时,如果有重写- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector则系统会调用该方法.如果上述signature = [surrogate methodSignatureForSelector:selector];返回的是nil,即另一个对象也不能响应该方法,则在执行forwardInvocation:方法前崩溃,因为forwardInvocation:方法的参数就是基于methodSignatureForSelector:的返回值创建的.

    重写方法签名方法有啥用?
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
    通过重写methodSignatureForSelector:方法可以事先判断出另一个对象是不是真的能够响应将被转发的消息.如果返回NO,那么就不用执行forwardInvocation:方法了.

    可以通过消息转发做一些简单的防崩溃.比如重写NSArray的相关方法.

    相关文章

      网友评论

        本文标题:iOS 消息转发

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