以下是个人对OC里面消息转发流程的理解,如有不正确的地方,欢迎指正。
@implementation MsgSendViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
OC里面调用一个方法的过程
首先去当前对象的缓存列表里面取,没有取到就去当前类的方法列表里面取,没有取到就去父类的方法列表里面取,没有取到就执行动态方法解析,没有成功就执行备援接受者,没有成功就执行消息转发
**/
objc_msgSend(self, NSSelectorFromString(@"print:"),@"2");
}
- (void)print:(id)para{
NSLog(@"存在对应的选择子就打印--->%@",para);
}
//打印结果
2018-07-24 23:08:46.456974+0800 Test[3147:350194] 存在对应的选择子就打印--->2
如果去掉那个实现的方法呢,就会报错,如下。其实在这个报错的前,还有三次补救的机会
//实例调用了未知的选择子,选择子就是方法
MsgSendViewController print:]: unrecognized selector sent to instance 0x7ffa29c0e7c0
通过runtime运行时,动态添加了一个未实现方法的实现,所以能够正常打印而不奔溃
+(BOOL)resolveInstanceMethod:(SEL)sel{
if ([@"print:" isEqualToString:NSStringFromSelector(sel)]){
IMP imp = class_getMethodImplementation([self class], @selector(setPrint:));
class_addMethod([self class], sel, imp, "v@:");
return true;
}
return [super resolveInstanceMethod:sel];
}
- (void)setPrint:(id)para{
NSLog(@"resolveInstanceMethod--->%@",para);
}
//打印结果
2018-07-24 23:16:07.363857+0800 Test[3287:369347] resolveInstanceMethod--->2
需要接收处理的备援接受者需要有对应选择子的方法
- (id)forwardingTargetForSelector:(SEL)aSelector{
if ([@"print:" isEqualToString:NSStringFromSelector(aSelector)]){
return _testMsg;
}
return [super forwardingTargetForSelector:aSelector];
}
//testMsg里面的有print方法的实现,能够调用这个print方法
#import "TestMsgSend.h"
@implementation TestMsgSend
- (void)print:(id)para{
NSLog(@"TestMsgSend---%@-----",para);
}
@end
//打印结果
2018-07-24 23:25:25.777116+0800 Test[3425:390946] TestMsgSend---2-----
3:)消息转发,methodSignatureForSelector 这个方法必须实现
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if (!signature){
signature = [TestMsgSend instanceMethodSignatureForSelector:aSelector];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation{
[anInvocation invokeWithTarget:_testMsg];
}
//TestMsgSend的实现
#import "TestMsgSend.h"
@implementation TestMsgSend
- (void)print:(id)para{
NSLog(@"TestMsgSend---%@-----",para);
}
@end
//打印结果
2018-07-24 23:37:24.548536+0800 Test[3626:420200] TestMsgSend---2-----
以下贴一下完整的代码,方法自上而下未实现的话就去找下面的实现。
- (void)viewDidLoad {
[super viewDidLoad];
/**
OC里面调用一个方法的过程
首先去当前对象的缓存列表里面取,没有取到就去当前类的方法列表里面取,没有取到就去父类的方法列表里面取,没有取到就执行动态方法解析,没有成功就执行备援接受者,没有成功就执行消息转发
**/
_testMsg = [TestMsgSend new];
objc_msgSend(self, NSSelectorFromString(@"print:"),@"2");
}
#pragma mark -- 自己实现的选择子
- (void)print:(id)para{
NSLog(@"存在对应的选择子就打印--->%@",para);
}
#pragma mark -- 动态方法解析
+(BOOL)resolveInstanceMethod:(SEL)sel{
if ([@"print:" isEqualToString:NSStringFromSelector(sel)]){
IMP imp = class_getMethodImplementation([self class], @selector(setPrint:));
class_addMethod([self class], sel, imp, "v@:");
return true;
}
return [super resolveInstanceMethod:sel];
}
- (void)setPrint:(id)para{
NSLog(@"resolveInstanceMethod--->%@",para);
}
#pragma mark -- 备援接受者
- (id)forwardingTargetForSelector:(SEL)aSelector{
if ([@"print:" isEqualToString:NSStringFromSelector(aSelector)]){
return _testMsg;
}
return [super forwardingTargetForSelector:aSelector];
}
#pragma maek -- 消息转发
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if (!signature){
signature = [TestMsgSend instanceMethodSignatureForSelector:aSelector];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation{
[anInvocation invokeWithTarget:_testMsg];
}
//TestMsgSend 接受处理消息的对象
#import
@interface TestMsgSend : NSObject
- (void)print:(id)para;
@end
#import "TestMsgSend.h"
@implementation TestMsgSend
- (void)print:(id)para{
NSLog(@"TestMsgSend---%@-----",para);
}
@end