消息传递出现在 当通过[receiver message]调用方法时,如果没有找到对应的方法处理消息,就进入消息传递,消息传递的方式如下:
- Method Resolution 动态加载
- Fast Forwarding 快速传递
- Normal Forwaeding 正常传递
三种方式的优先级:Method Resolution > Fast Forwarding > Normal Forwaeding
Method Resolution
动态加载,当消息没找对应的方法处理,系统就会调用receiver的resolveClassMethod:
或者resolveInstanceMethod:
让你添加方法实现,如果实现了这个方法,系统就会重新启动一次消息发送。
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
NSLog(@"Method Resolution");
if (sel == @selector(method)) { // 方法没有被实现
class_addMethod([self class], sel, imp_implementationWithBlock(^() {
// 实现方法的代码写在这里
}), "v@:");
return YES;
}
return [super resolveInstanceMethod:sel];
}
Fast Forwarding
快速传递,当Method Resolution没有成功处理消息,系统就会调用receiver的forwardingTargetForSelector:
方法来确定是哪个对象处理(找到该对象的方法名与消息中的选择器的方法名一致的方法并调用)这个消息。
注意:forwardingTargetForSelector:
如果实现这个方法时,返回值为nil
或者self
即代表不处理消息,进入Normal Forwaeding
- (id)forwardingTargetForSelector:(SEL)aSelector
{
NSLog(@"Fast Forwarding");
// 返回处理这个方法的对象,返回nil或者self表示消息不处理,进入Normal Forwaeding
return _customView;
}
Normal Forwaeding
正常传递 当Fast Forwarding
和 Method Resolution
都没有处理消息时,系统就会先调用methodSignatureForSelector:
获取方法签名,如果返回值为nil
,则消息处理失败,程序崩溃。反正方法签名对象后,系统会调用forwardInvocation:
进行消息处理。
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
NSLog(@"Normal Forwarding");
// 自己定制invocation的选择器的方法
anInvocation.selector = @selector(handleError);
if ([self respondsToSelector:[anInvocation selector]])
{
[anInvocation invokeWithTarget:self];
}
else
[super forwardInvocation:anInvocation];
}
总结
是不是有点迷糊了? 不要担心,我们来看一下图,因为图片比文字表现得更直观,相信这张图能让大家对三种消息传递方法之间的关系更加了解。
最后,如果上面三种方式都没有处理,系统就会崩溃,报对象没有找到的错误:
-[ViewController method]: unrecognized selector sent to instance 0x7fea0ac2b0a0