iOS - runtime如何通过selector 找到对应的 IMP 地址(分别考虑类方法和实例方法)

序言

对象中有类方法和实例方法的列表,列表中记录着方法的名词、参数和实现,而selector本质就是方法名称,runtime通过这个方法名称就可以在列表中找到该方法对应的实现。

struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        
    const char *name                                         
    long version                                             
    long info                                                
    long instance_size                                       
    struct objc_ivar_list *ivars                             
    struct objc_method_list **methodLists                    
    struct objc_cache *cache                                 
    struct objc_protocol_list *protocols                     
#endif

} OBJC2_UNAVAILABLE;

这里声明了一个指向struct objc_method_list指针的指针,可以包含类方法列表和实例方法列表

具体实现

在寻找IMP的地址时,runtime提供了两种方法

IMP class_getMethodImplementation(Class cls, SEL name);
IMP method_getImplementation(Method m)

第一种方法

对于第一种方法而言,类方法和实例方法实际上都是通过调用class_getMethodImplementation()来寻找IMP地址的,不同之处在于传入的第一个参数不同。
类方法(假设有一个类 A)

class_getMethodImplementation(objc_getMetaClass("A"),@selector(methodName));

实例方法

class_getMethodImplementation([A class],@selector(methodName));

通过该传入的参数不同,找到不同的方法列表,方法列表中保存着下面方法的结构体,结构体中包含这方法的实现,selector本质就是方法的名称,通过该方法名称,即可在结构体中找到相应的实现。

struct objc_method {
    SEL method_name                                      
    char *method_types                                       
    IMP method_imp                                           
}

第二种方法

传入的参数只有method,区分类方法和实例方法在于封装method的函数
类方法

Method class_getClassMethod(Class cls, SEL name)

实例方法

Method class_getInstanceMethod(Class cls, SEL name)

最后调用IMP method_getImplementation(Method m) 获取IMP地址

代码实战

在Test.m 实现文件中

#import "Test.h"
#import <objc/runtime.h>

@implementation Test

- (instancetype)init {
    self = [super init];
    if (self) {
        [self getIMPFromSelector:@selector(aaa)];
        [self getIMPFromSelector:@selector(test1)];
        [self getIMPFromSelector:@selector(test2)];
    }
    return self;
}

- (void)test1 {
    NSLog(@"test1");
}

- (void)test2 {
    NSLog(@"test2");
}

- (void)getIMPFromSelector:(SEL)aSelector {
    // first method
    IMP instanceIMP1 = class_getMethodImplementation(objc_getClass("Test"), aSelector);
    IMP classIMP1 = class_getMethodImplementation(objc_getMetaClass("Test"), aSelector);
    
    // second method
    Method instanceMethod = class_getInstanceMethod(objc_getClass("Test"), aSelector);
    IMP instanceIMP2 = method_getImplementation(instanceMethod);
    
    Method classMethod1 = class_getClassMethod(objc_getClass("Test"), aSelector);
    IMP classIMP2 = method_getImplementation(classMethod1);
    
    Method classMethod2 = class_getClassMethod(objc_getMetaClass("Test"), aSelector);
    IMP classIMP3 = method_getImplementation(classMethod2);
    
    NSLog(@"instance1:%p instance2:%p class1:%p class2:%p class3:%p",instanceIMP1,instanceIMP2,classIMP1,classIMP2,classIMP3);
}

@end

这里有一个叫 Test 的类,在初始化方法里,调用了两次getIMPFromSelector:方法,第一个 aaa 方法不存在,test1和test2分别为实例方法和类方法。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    Test *test1 = [[Test alloc] init];
    Test *test2 = [[Test alloc] init];
}

同时实例化了两个Test的对象,打印信息如下
输出信息如下:

image.png

大家注意图中红色标注的地址出现了8次:0x1103a4600,这个是在调用class_getMethodImplementation()方法时,无法找到对应实现时返回的相同的一个地址,无论该方法是在实例方法或类方法,无论是否对一个实例调用该方法,返回的地址都是相同的,但是每次运行该程序时返回的地址并不相同,而对于另一种方法,如果找不到对应的实现,则返回0,在图中我做了蓝色标记。

还有一点有趣的是class_getClassMethod()的第一个参数无论传入objc_getClass()还是objc_getMetaClass(),最终调用method_getImplementation()都可以成功的找到类方法的实现。

而class_getInstanceMethod()的第一个参数如果传入objc_getMetaClass(),再调用method_getImplementation()时无法找到实例方法的实现却可以找到类方法的实现。

本文参考runtime如何通过selector找到对应的IMP地址?(分别考虑类方法和实例方法)
非常感谢该作者!!!

项目连接地址


更多有关runtime文章请看下面
iOS-runtime-API详解+使用
iOS - runtime -详解
iOS Runtime原理及使用
iOS - Runtime之面试题详解一
iOS-runtime之面试题详解二
iOS runtime的使用场景-实战篇



作者:路飞_Luck
链接:https://www.jianshu.com/p/8b6a723e22e5
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/li198847/article/details/88172989
imp