美文网首页程序员
iOS 消息发送、转发机制简述

iOS 消息发送、转发机制简述

作者: zzchsppy | 来源:发表于2016-05-06 14:26 被阅读360次

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

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

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

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

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

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

    所以我们在重写methodSignatureForSelector的时候就人工让其返回有效实例。

    我们定义了这样一个类

    @interface TargetProxy : NSProxy {

    id realObject1;

    id realObject2;

    }

    - (id)initWithTarget1:(id)t1 target2:(id)t2;

    @end

    实现:

    @implementation TargetProxy

    - (id)initWithTarget1:(id)t1 target2:(id)t2 {

    realObject1 = [t1 retain];

    realObject2 = [t2 retain];

    return self;

    }

    - (void)dealloc {

    [realObject1 release];

    [realObject2 release];

    [super dealloc];

    }

    // The compiler knows the types at the call site but unfortunately doesn't

    // leave them around for us to use, so we must poke around and find the types

    // so that the invocation can be initialized from the stack frame.

    // Here, we ask the two real objects, realObject1 first, for their method

    // signatures, since we'll be forwarding the message to one or the other

    // of them in -forwardInvocation:.  If realObject1 returns a non-nil

    // method signature, we use that, so in effect it has priority.

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {

    NSMethodSignature *sig;

    sig = [realObject1 methodSignatureForSelector:aSelector];

    if (sig) return sig;

    sig = [realObject2 methodSignatureForSelector:aSelector];

    return sig;

    }

    // Invoke the invocation on whichever real object had a signature for it.

    - (void)forwardInvocation:(NSInvocation *)invocation {

    id target = [realObject1 methodSignatureForSelector:[invocation selector]] ? realObject1 : realObject2;

    [invocation invokeWithTarget:target];

    }

    // Override some of NSProxy's implementations to forward them...

    - (BOOL)respondsToSelector:(SEL)aSelector {

    if ([realObject1 respondsToSelector:aSelector]) return YES;

    if ([realObject2 respondsToSelector:aSelector]) return YES;

    return NO;

    }

    @end

    现在我们还用这个类,注意向它发送的消息:

    id proxy = [[TargetProxy alloc] initWithTarget1:string target2:array];

    // Note that we can't use appendFormat:, because vararg methods

    // cannot be forwarded!

    [proxy appendString:@"This "];

    [proxy appendString:@"is "];

    [proxy addObject:string];

    [proxy appendString:@"a "];

    [proxy appendString:@"test!"];

    NSLog(@"count should be 1, it is: %d", [proxy count]);

    if ([[proxy objectAtIndex:0] isEqualToString:@"This is a test!"]) {

    NSLog(@"Appending successful.");

    } else {

    NSLog(@"Appending failed, got: '%@'", proxy);

    }

    运行的结果是:

    count should be 1, it is:  1

    Appending successful.

    TargetProxy声明中是没有appendString与addObject消息的,在这儿却可以正常发送,不crash,原因就是发送消息的时候,如果原本类没有这个消息响应的时候,转向询问methodSignatureForSelector,接着在forwardInvocation将消息重定向。

    相关文章

      网友评论

        本文标题:iOS 消息发送、转发机制简述

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