Message Forwarding
void forwardInvocation:(NSInvocation *) anInvocation…
若调用一个对象没有实现的方法时,在报告一个错误之前,运行时会先调用一个forwardInvocation:方法,该方法会带有一个封装了原始调用的message以及对应的参数。
要forward一个message,所有forwardInvacation的实现必须要:
1. 决定这个message需要发到哪里
2. 将原始的参数也一并发送
- (void) forwardInvocation: (NSInvocation *) anInvocation{
if([someOtherObject respondsToSelector: [anInvocation selector]])
[anInvocation invokeWithTarget: someOtherObject];
else
[super forwardInvocation:anInvocation];
}
注意:forwardInvocation只会在你调用了一个对象的不存在的method才会触发。
forwarding 可以模拟多重继承;也可以用来开发轻量级别的代理…