【Preprocess】
在使用forwarding机制前,会先经历2个步骤,只有当这2个步骤均失败的情况下,才会激活forwarding。
1、+(BOOL)resolveInstanceMethod:(SEL)selector、resolveClassMethod。
当第一次没找到SEL时,调用上述两方法之一。如果在上述2方法中加入了方法,并返回YES,则会尝试重新解析此SEL。
2、-(id)forwardingTargetForSelector:(SEL)selector,当上述第1条机制失败后,本条机制会被激活。此方法返回能够处理SEL的对象。如果返回非nil,则会调用此对象的SEL。此种机制无法定制参数。
当上述2个机制均失败后,会起用forwarding机制,如下。
【Message Forwarding】
If you send a message to an object that does not handle that message, before announcing an error the runtime sends the object a forwardInvocation: message with an NSInvocation object as its sole argument—the NSInvocation object encapsulates the original message and the arguments that were passed with it.
forwardInvocation方法可以帮助我们轻松的实现完美转发。
1 - (void)forwardInvocation:(NSInvocation *)anInvocation 2 { 3 if ([someOtherObject respondsToSelector: 4 [anInvocation selector]]) 5 [anInvocation invokeWithTarget:someOtherObject]; 6 else 7 [super forwardInvocation:anInvocation]; 8 }
forwarding实际上提供了一种多生继承的变相实现。返回值被存储在NSInvocation中,NSObject框架会把NSInvocation中的返回值返回给原调用者。
Although forwarding mimics inheritance, the NSObject class never confuses the two. Methods like respondsToSelector: and isKindOfClass: look only at the inheritance hierarchy, never at the forwarding chain. If, for example, a Warrior object is asked whether it responds to a negotiate message,
1 if ( [aWarrior respondsToSelector:@selector(negotiate)] ) 2 ...
the answer is NO, even though it can receive negotiate messages without error and respond to them.
如果想让respondsToSelector对代理的方法也返回YES,可以按如下实现:
1 - (BOOL)respondsToSelector:(SEL)aSelector 2 { 3 if ( [super respondsToSelector:aSelector] ) 4 return YES; 5 else { 6 /* Here, test whether the aSelector message can * 7 * be forwarded to another object and whether that * 8 * object can respond to it. Return YES if it can. */ 9 } 10 return NO; 11 }
if an object forwards any remote messages it receives, it should have a version of methodSignatureForSelector: that can return accurate descriptions of the methods that ultimately respond to the forwarded messages
1 - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector 2 { 3 NSMethodSignature* signature = [super methodSignatureForSelector:selector]; 4 if (!signature) { 5 signature = [surrogate methodSignatureForSelector:selector]; 6 } 7 return signature; 8 }