iOS IMP

作者: 想飞的菜鸟 | 来源:发表于2020-10-12 11:20 被阅读0次

IMP是”implementation”的缩写,存储的是OC实现方法代码块的地址。一般通过[obj method:params] 或者 objc_msgSend()向对象发送消息,然后OC的runtime机制会通过SEL找到对应的IMP指针,继而调用它所指向的函数实现。

一、objc_method数据结构
进入runtime.h的内部,可以看到objc_method的数据结构如下:

struct objc_method {
SEL _Nonnull method_name OBJC2_UNAVAILABLE; //方法名
char * _Nullable method_types OBJC2_UNAVAILABLE; //方法类型
IMP _Nonnull method_imp OBJC2_UNAVAILABLE; //具体方法实施的指针
} OBJC2_UNAVAILABLE;

对应的获取:
SEL method_getName(Method method);
IMP method_getImplementation(Method method);
const char * ivar_getTypeEncoding(Ivar ivar);

二、获取当前方法的默认IMP
NSObject对象提供了两个方法来获取对应的IMP指针

  • (IMP)methodForSelector:(SEL)aSelector;
  • (IMP)instanceMethodForSelector:(SEL)aSelector;

1.使用methodForSelector方法(methodForSelector返回的IMP是default IMP,即发送消息时会调用的IMP)
(1)若向类(class)发送消息,则aSelector应该是类方法(class method);
(2)若向实例对象(instance)发送消息,则aSelector应该为实例对象方法(instance method)。
2.使用instanceMethodForSelector可向类请求实例方法的IMP。

获取当前方法的IMP,可使用self对象和隐含的_cmd参数。
隐含参数:尽管这些参数不是显式声明的,源码仍能引用它们(正像它能引用接收对象的实例变量一样)。
_cmd 是隐藏的参数,表示当前方法的selector。
作用:
1、获取当前被调用方法:NSStringFromSelector(_cmd)。
2、在运行时时使用:在某个分类方法里为对象动态添加属性,由于_cmd是在编译时候(compile - time)就可以确定的值,因此可以直接使用。

IMP current = [[self class] instanceMethodForSelector:_cmd];

链接:
1.https://www.jianshu.com/p/a757adc81a96
2.https://www.cnblogs.com/someonelikeyou/p/8560156.html
3.https://juejin.im/post/6844903939586916366

相关文章

  • iOS runtime通过selector获取IMP地址

    iOS runtime通过selector获取IMP地址 获取IMP地址有两种方法: class_getMetho...

  • iOS IMP

    IMP是”implementation”的缩写,存储的是OC实现方法代码块的地址。一般通过[obj method:...

  • iOS IMP

    一.什么是IMP IMP是”implementation”的缩写,它是objetive-C 方法(method)实...

  • 自定义tabbar

    关于iOS自定义UITabBar的几种方法 #import "CDTabBarController.h" #imp...

  • iOS IMP和SEL

    SEL : 类成员方法的指针,但不同于C语言中的函数指针,函数指针直接保存了方法的地址,但SEL只是方法编号。 I...

  • iOS IMP 与 SEL

    IMP 官方文档 SEL 官方文档 sel_registerName 通过SEL 找到对应的IMP并调用 meth...

  • iOS【IMP 与 SEL】

    摘录:ChinaChong FunctionNSSelectorFromStringReturns the sel...

  • iOS 消息转发机制

    上节(iOS 消息查找流程)我们讲到,在iOS中对象调用方法,会经历方法的查找,如果查找到方法的IMP,那么就返回...

  • iOS进阶之Runtime----方法---type

    iOS进阶之Runtime----方法---type Type:方法的签名 SEL:方法的名称 IMP:方法的指针...

  • iOS 正则表达式判断手机号,邮箱等

    iOS正则表达式判断手机号,邮箱等 #import "NSString+RegexCategory.h" @imp...

网友评论

      本文标题:iOS IMP

      本文链接:https://www.haomeiwen.com/subject/qaosuktx.html