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