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

作者:代培
地址:http://daipei.me/posts/how_to_get_imp_address_by_selector/
转载请注明出处
我的博客搬家了,新博客地址:daipei.me

最近在看《招聘一个靠谱的iOS》,这是其中的一个题目,看着别人的解答不是很详细,于是就想弄清楚一些,通过查找了一些资料并且自己写了一些测试的代码,在这里做个总结!

概述

类对象中有类方法和实例方法的列表,列表中记录着方法的名词、参数和实现,而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)

而根据官方描述,第一种方法可能会更快一些

@note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).

对于第一种方法而言,类方法和实例方法实际上都是通过调用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的类,在初始化方法里,调用了两次getIMPFromSelector:方法,第一个aaa方法是不存在的,test1和test2分别为实例方法和类方法

然后我同时实例化了两个Test的对象,打印信息如下

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

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

详细参考我后面的一篇博客Objective-C runtime源码学习之IMP寻址

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,Selector是一个用于定义不同状态下View的样式和背景的XML文件。它可以根据View的不同状态(如按下、选中、禁用等)来设置不同的样式和背景。下面是Selector的用法详解及实例。 1. 创建selector xml文件 在res/drawable目录下创建一个XML文件,文件名以selector_开头,如selector_button.xml,然后在文件中定义不同状态下View的样式和背景。例如: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <!-- 选中状态 --> <item android:drawable="@drawable/button_selected" android:state_selected="true" /> <!-- 默认状态 --> <item android:drawable="@drawable/button_normal" /> </selector> ``` 2. 在View中应用selector 在布局文件中使用android:background属性来应用selector,如: ```xml <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:background="@drawable/selector_button" /> ``` 这样,当Button的状态改变时,它的背景就会自动变化。 除了android:background属性外,还可以在TextView、EditText等View中使用android:textColor属性来应用selector,如: ```xml <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="@drawable/selector_text_color" /> ``` 其中,selector_text_color.xml文件的内容如下: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:color="@color/colorAccent" android:state_pressed="true" /> <!-- 选中状态 --> <item android:color="@color/colorPrimary" android:state_selected="true" /> <!-- 默认状态 --> <item android:color="@android:color/black" /> </selector> ``` 这样,当TextView的状态改变时,它的文字颜色就会自动变化。 以上就是Selector在Android中的用法详解及实例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值