美文网首页ios runtime专题
Runtime | 方法交换

Runtime | 方法交换

作者: 字符管理师 | 来源:发表于2019-08-04 21:41 被阅读0次

准备工作/ 创建Person类并声明两个方法

- (void) fristMethod;
- (void) secondMethod;

方法简单实现

- (void) fristMethod {
    NSLog(@"fristMethod");
}
- (void) secondMethod {
    NSLog(@"secondMethod");
}

控制器调用

///  使用runtime来交换两个方法
- (void) rylsj_ExchangedMethod {
    /// 获取方法
    Method m1 = class_getInstanceMethod([self.persion class], @selector(fristMethod));
    Method m2 = class_getInstanceMethod([self.persion class], @selector(secondMethod));
    /// 交换
    method_exchangeImplementations(m1, m2);
}

注意

method_exchangeImplementations 交换SEL 和 IMP 的对应关系
SEL -- IMP(才是指针!)
class_getInstanceMethod : 获取实例方法
class_getClassMethod 获取类方法

相关文章

  • runtime

    runtime交换方法 动态添加方法

  • runTime常用方法

    使用runTime改变实例成员的值 使用runtime来交换两个方法 注意再次调用该方法不交换 使用runTime...

  • Runtime

    runtime运行时机制1:通过runtime,实现方法交换(交换两个类方法、交换两个实例方法)2:通过runti...

  • Day3

    1 runtime运行时机制1:通过runtime,实现方法交换(交换两个类方法、交换两个实例方法)。2:通过ru...

  • runtime的理解(二)

    主要内容 利用 runtime 交换方法 利用 runtime 动态添加方法 利用 runtime 动态添加属性 ...

  • 查看SDK调用支付宝参数

    使用runtime 方法交换openurl

  • objc runtime (四)动态添加属性

    在《objc runtime (二)交换方法》中我提到过runtime最实用的就是交换方法和动态添加属性两个用法。...

  • iOS runtime如何交换两个类方法

    如有转载,请标明出处:iOS runtime如何交换两个类方法 runtime交换实例方法,老生常谈的问题,很多b...

  • iOS -- runtime的应用

    runtime主要有一下几种应用场景 方法交换 添加属性 (一)方法交换 (1)字体适配 方法交换实际交换的是方法...

  • runtime和oc内存区域(2018-04-02)

    runtime常用的几个方法: 交换方法 动态添加属性 动态添加方法 1.交换方法 class_getClassM...

网友评论

    本文标题:Runtime | 方法交换

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