在obj-c中我們可以向一個(gè)實(shí)例發(fā)送消息昭殉,相當(dāng)于c/c++ java中的方法調(diào)用,只不過在這兒是說發(fā)送消息,實(shí)例收到消息后會進(jìn)行一些處理挪丢。比如我們想調(diào)用一個(gè)方法蹂风,便向這個(gè)實(shí)例發(fā)送一個(gè)消息,實(shí)例收到消息后乾蓬,如果能respondsToSelector惠啄,那么就會調(diào)用相應(yīng)的方法。如果不能respond一般情況下會crash巢块。今天要的礁阁,就是不讓它c(diǎn)rash。
首先說一下向一個(gè)實(shí)例發(fā)送一個(gè)消息后族奢,系統(tǒng)是處理的流程:
1. 發(fā)送消息如:[self startwork]
2. 系統(tǒng)會check是否能response這個(gè)消息
3. 如果能response則調(diào)用相應(yīng)方法姥闭,不能則拋出異常
在第二步中,系統(tǒng)是如何check實(shí)例是否能response消息呢越走?如果實(shí)例本身就有相應(yīng)的response,那么就會相應(yīng)之棚品,如果沒有系統(tǒng)就會發(fā)出methodSignatureForSelector消息,尋問它這個(gè)消息是否有效廊敌?有效就返回對應(yīng)的方法地址之類的铜跑,無效則返回nil。如果是nil,Runtime則會發(fā)出-doesNotRecognizeSelector:消息骡澈,程序這時(shí)也就掛掉了.如果不是nil接著發(fā)送forwardInvocation消息锅纺。
所以我們在重寫methodSignatureForSelector的時(shí)候就人工讓其返回有效實(shí)例。
我們定義了這樣一個(gè)類
@interface TargetProxy : NSProxy {
id realObject1;
id realObject2;
}
- (id)initWithTarget1:(id)t1 target2:(id)t2;
@end
實(shí)現(xiàn):
@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
現(xiàn)在我們還用這個(gè)類肋殴,注意向它發(fā)送的消息:
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);
}
運(yùn)行的結(jié)果是:
count should be 1, it is: ?1
Appending successful.
TargetProxy聲明中是沒有appendString與addObject消息的囤锉,在這兒卻可以正常發(fā)送,不crash护锤,原因就是發(fā)送消息的時(shí)候官地,如果原本類沒有這個(gè)消息響應(yīng)的時(shí)候,轉(zhuǎn)向詢問methodSignatureForSelector烙懦,接著在forwardInvocation將消息重定向驱入。