美文网首页
runtime用法

runtime用法

作者: 圣僧留步 | 来源:发表于2017-12-26 11:50 被阅读14次

    在之前学习runtime的过程中,我发现方法交换有两种写法,一开始对对一种写法不太能理解,后来自己写demo来试验了一下以后就知道他是怎么回事了,其实这不是两种写法,准确的来说,只是这样写更严谨一点。
在做试验之前,首先你得了解以下三个方法的具体作用

  • class_addMethod(添加方法,如方法已经存在,则添加失败)
  • class_replaceMethod(替换方法)
  • method_exchangeImplementations(方法交换,使用前提,两个方法都必须存在)

下面给出代码:

@interface ViewController : UIViewController

- (void)systemMethod_PrintLog;
- (void)ll_imageName;
 @end



+(void)load{

SEL originalSelector = @selector(systemMethod_PrintLog);
SEL swizzledSelector = @selector(ll_imageName);

Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);

BOOL didAddMethod =
class_addMethod(self,
                originalSelector,
                method_getImplementation(swizzledMethod),
                method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
    class_replaceMethod(self,
                        swizzledSelector,
                        method_getImplementation(originalMethod),
                        method_getTypeEncoding(originalMethod));
} else {
    method_exchangeImplementations(originalMethod, swizzledMethod);
}
}


- (void)ll_imageName{

NSLog(@"愁啊愁,白了头");
}

- (void)viewDidLoad {
[super viewDidLoad];
[self ll_imageName];
}

从上面我们可以看到,我只是写了ll_imageName这个方法的实现,原来的方法systemMethod_PrintLog我并没有写它的实现。那么因为此时originalSelector方法选择器中的实现是不存在的,所以class_addMethod方法就将ll_imageName方法的实现给加入到originalSelector方法选择器中,此时相当于让A的去接收B的实现。然后走class_replaceMethod方法,将swizzledSelector选择器的实现跟originalSelector的实现互相交换,看到这里,你应该已经猜到,无论如何交换,A,B两个选择器的实现都是一样的。

我们看看分别调用两个方法的结果
1.调用systemMethod_PrintLog,从图中可以看到实际执行的是ll_imageName的实现。



2.调用ll_imageName,从图中弄可以看到,它还是执行的是ll_imageName的实现。


结论:当原方法只是声明了并没实现时,咱们需要对其做处理,处理的方法就是把需要替换的方法实现给原方法。这种情况下,调用两个方法中的任何一个方法,他们的实现都是走的替换方法。

另一种情况就是当两个方法都存在的时候,就走下面的method_exchangeImplementations来进行方法的交换

总结:方法交换的这两种方法其实,replaceMethod是对原方法没有实现的一种补充说明。在写代码的过程中,尽量咱们应该尽量写全。

相关文章

  • iOS面试点文章链接

    runtime基础方法、用法、消息转发、super: runtime 完整总结 runloop源码、runloop...

  • RunTime用法

    一、什么是RunTime iOS开发过程中,我们一直在与Runtime打交道,可什么是Runtime呢? 对比C语...

  • runtime用法

    在之前学习runtime的过程中,我发现方法交换有两种写法,一开始对对一种写法不太能理解,后来自己写demo来试验...

  • runtime用法

    1.监听网络状态 2.添加分类属性(set / get_assocaite关联) 3.获取类的成员变量以及属性等(...

  • iOS-runtime相关

    本篇涵盖runtime的解析、应用等. 1.runtime快速入门和实战2.Runtime 10种用法(没有比这更...

  • Docker 命令

    用法 Usage: A self-sufficient runtime for containers. 选项 Op...

  • Runtime消息转发及其应用

      之前写过文章Runtime的常见用法里面有介绍过利用Objective-C的Runtime特性来给Catego...

  • Runtime快速上手(1)

    依照惯例跳过Runtime的介绍,这里只介绍Runtime的基本用法和简单应用例子。 一、获取属性列表 输出结果:...

  • runtime的用法

    1.使用runtime改变变量值 2.使用runtime交换方法 3.使用runtime添加方法 4.使用runt...

  • runtime的用法

    最近在研究runtime 主要是想好好的装一波 无奈 看过了 唐巧大大的runtime介绍 看的我是脑袋直疼,看来...

网友评论

      本文标题:runtime用法

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