0
回答
NSObject中forwardInvocation消息重定向
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

NSObject是大多数系统 api的基类,今天打开头文件看了一下,原来它还有很多现在都还不了解的地方。今天简单介绍一下它的forwardInvocation功能。


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


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

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

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

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


在第二步中,系统是如何check实例是否能response消息呢?如果实例本身就有相应的response,那么就会相应之,如果没有系统就会发出methodSignatureForSelector消息,寻问它这个消息是否有效?有效就返回对应的方法地址之类的,无效则返回nil。如果是nil就会crash, 如果不是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

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

// Create a proxy to wrap the real objects.  This is rather
    // artificial for the purposes of this example -- you'd rarely
    // have a single proxy covering two objects.  But it is possible.
    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将消息重定向。 上面也说了多参数的消息是不能重定向的。这我还没测过。



reference:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html#//apple_ref/doc/uid/TP40008048-CH105-SW2

http://developer.apple.com/library/mac/#samplecode/ForwardInvocation/Listings/main_m.html#//apple_ref/doc/uid/DTS40008833-main_m-DontLinkElementID_4

http://cocoawithlove.com/2008/03/construct-nsinvocation-for-any-message.html





原文链接:http://blog.csdn.net/favormm/article/details/7418022
<无标签>
举报
长平狐
发帖于5年前 0回/450阅
顶部