OC Runtime消息转发 笔记

推荐阅读:
iOS Runtime详解
为什么Objective-C的消息转发要设计三个阶段?

这篇文章是我阅读👆文章和官方文档时的一些笔记,记录一下学习的过程和疑惑的地方

消息转发机制发生在消息传递机制搜索结束仍找不到方法的实现时,让消息传递机制外的类/对象有机会提供方法的实现

消息转发有三个阶段:

  • 动态方法解析
  • 转发实现的对象
  • 转发调用方式
Runtime消息转发

iOS Runtime详解的测试代码有困惑,为什么resolveInstanceMethod返回Yes,还会进入forwardingTargetForSelector,后来发现返回Yes是再次寻找方法的实现,如果最后找不到,仍会进入下一阶段,所以在原图的基础上做了一些补充,方便理解。

另外,第二阶段forwardingTargetForSelectorreturn的方式只能转发一个对象,第三阶段的NSInvocation可以把消息转发给多个对象。

虽然我们常说消息转发有三个阶段,但是官方文档上定义的消息转发特指图中第三阶段转发调用方式

PS:NSObject默认实现了forwardInvocation,不做任何转发,报错unrecognized selector
-(void)forwardInvocation:(NSInvocation *)anInvocation{
[self doesNotRecognizeSelector:anInvocation.selector];
}

测试

DEMO 提供了完整转发过程代码和三个阶段单独的测试代码
测试代码参考自iOS Runtime详解官方文档,为了方便理解做了一些修改,仅用于记录自己的测试情况和提供参考,更详细易懂的说明可以看推荐的文章

整个转发的过程:
如果当前类/对象在自己和父类的方法列表里都找不到方法的实现,消息传递结束,开始消息转发:

  • 第一个阶段,尝试通过resolveInstanceMethod获取动态的方法实现
  • 若第一阶段找不到,尝试通过forwardingTargetForSelector到另一个类/对象寻找
  • 若第二阶段的类/对象也没有找到,尝试通过methodSignatureForSelectorforwardInvocation把方法调用的名字,参数,返回值等信息,转发给多个类/对象
  • 三个阶段都没找到,出现unrecognized selector提示

动态方法解析

官方文档
通过在resolveInstanceMethod方法中调用class_addMethod来给对象动态添加方法的实现

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //执行foo函数
    [self performSelector:@selector(foo)];
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    if (sel == @selector(foo)){
        class_addMethod([self class], sel, (IMP) dynamicMethodIMP, "v@:");
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

void dynamicMethodIMP(id self, SEL _cmd)
{
    NSLog(@"Doing dynamic foo");
}

打印结果:
Doing dynamic foo

转发实现的对象

当前对象没有对应的实现,就让其他有实现的对象来帮忙处理
forwardingTargetForSelector方法return另一个对象来帮忙处理

@interface Person: NSObject

@end

@implementation Person

- (void)foo {
    NSLog(@"Doing Person foo");//Person的foo函数
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //执行foo函数
    [self performSelector:@selector(foo)];
}

- (id)forwardingTargetForSelector:(SEL)aSelector{
    if (aSelector == @selector(foo)){
        Person *person = [Person new];
       
        return person;
    }else{
        return [super forwardingTargetForSelector:aSelector];
    }
}

打印结果:
Doing Person foo

转发调用方式

官方文档
通过methodSignatureForSelector生成方法的签名,传给forwardInvocation,在forwardInvocation中由NSInvocation把方法名,参数,返回值等信息转发给新的对象。

@interface Person: NSObject

@end

@implementation Person

- (void)foo {
    NSLog(@"Doing Person foo");//Person的foo函数
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //执行foo函数
    [self performSelector:@selector(foo)];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    if (aSelector == @selector(foo)){
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
    return [super methodSignatureForSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation{
    if (anInvocation.selector == @selector(foo)){
        Person *person = [Person new];
        [anInvocation invokeWithTarget:person];
    }else{
        [super forwardInvocation:anInvocation];
    }
}

打印结果:
Doing Person foo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。