美文网首页
runtime方法交换

runtime方法交换

作者: Chris_C | 来源:发表于2017-02-22 07:04 被阅读10次

方法交换的作用:
一、在不修改源代码的基础上,对方法内的代码进行修改
二、

- (void)viewDidLoad {
[super viewDidLoad];
objc_msgSend(self,@selector(test));

[self say];
[self mySay];

[self changeMethod];

[self say];
[self mySay];
}

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

- (void)changeMethod
{
    /*
     class_getInstanceMethod     得到类的实例方法
     class_getClassMethod          得到类的类方法
     */
    Method say1 = class_getInstanceMethod([ViewController    class], @selector(say));
    Method mySay1 = class_getInstanceMethod([ViewController class], @selector(mySay));
    method_exchangeImplementations(say1, mySay1);
}

- (void)say
{
     NSLog(@"%s",__FUNCTION__);
}

- (void)mySay
{
    NSLog(@"%s",__FUNCTION__);
}

测试结果:
2016-08-11 16:17:16.997 RuntimeTest[3199:126416] test
2016-08-11 16:17:16.997 RuntimeTest[3199:126416] -    [ViewController say]
2016-08-11 16:17:16.997 RuntimeTest[3199:126416] - [ViewController mySay]
2016-08-11 16:17:16.998 RuntimeTest[3199:126416] -[ViewController mySay]
2016-08-11 16:17:16.998 RuntimeTest[3199:126416] -[ViewController say]

相关文章

  • 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/gplssttx.html