前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >runtime如何通过selector找到对应的IMP地址?

runtime如何通过selector找到对应的IMP地址?

作者头像
赵哥窟
发布2018-09-13 11:43:53
1.6K0
发布2018-09-13 11:43:53
举报
文章被收录于专栏:日常技术分享日常技术分享

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

代码语言:javascript
复制
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

代码语言:javascript
复制
- (void)getIMP_class_getMethodImplementationFromSelector:(SEL)aSelector{
    
    const char *className = object_getClassName([self class]);
    // 获取实例的IMP
    IMP instanceIMP = class_getMethodImplementation(objc_getClass(className), aSelector);
    // 获取类的IMP
    IMP classIMP = class_getMethodImplementation(objc_getMetaClass(className), aSelector);
    
    NSLog(@"instanceIMP:%p classIMP:%p",instanceIMP,classIMP);
}

对于第一种方法而言,类方法和实例方法实际上都是通过调用class_getMethodImplementation()来寻找IMP地址的

第二种方法:method_getImplementation

代码语言:javascript
复制
- (void)getIMP_method_getImplementationFromSelector:(SEL)aSelector{
    
    const char *className = object_getClassName([self class]);
    // 获取类中的某个实例方法
    Method instanceMethod = class_getInstanceMethod(objc_getClass(className), aSelector);
    // 获取类中的某个类方法
    Method classMethod = class_getClassMethod(objc_getClass(className), aSelector);
    
    // 获取实例的IMP
    IMP instanceIMP = method_getImplementation(instanceMethod);
    // 获取类的IMP
    IMP classIMP = method_getImplementation(classMethod);
    
    NSLog(@"instanceIMP:%p classIMP:%p",instanceIMP,classIMP);
}

而method_getImplementation而言,传入的参数只有method,区分类方法和实例方法在于封装method的函数 类方法 Method class_getClassMethod(Class cls, SEL name) 实例方法 Method class_getInstanceMethod(Class cls, SEL name)

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

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

代码语言:javascript
复制
struct objc_method {
    SEL method_name                                      
    char *method_types                                       
    IMP method_imp                                           
}

Demo:https://github.com/destinyzhao/SelectorFindIMP

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在寻找IMP的地址时,runtime提供了两种方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档