基本原理知识

iOS +(void)load运行流程解析和运用总结

2017-12-04  本文已影响1106人  _铲屎官_

最近因为机缘巧合接触到了+(void)load方法,通过查看了几篇博客,对load方法有了一定的认识,今天通过该文章对该方法进行一些小总结。


镇楼图.jpeg

+(void)load

官方是这么定义+(void)load方法的

Discussion
The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.
The order of initialization is as follows:
1.All initializers in any framework you link to.
2.All +load methods in your image.(这个image指的就是二进制可执行文件)
3.All C++ static initializers and C/C++ __attribute__(constructor) functions in your image.
4.All initializers in frameworks that link to you.
In addition:
1.A class’s +load method is called after all of its superclasses’ +load methods.
2.A category +load method is called after the class’s own +load method.
In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.

由此我们可以知道load函数是只要你动态加载或者静态引用了这个类,那么load就会被执行,它并不需要你显示的去创建一个类后才会执行,同时只执行一次。

另外就是关于load的执行顺序问题,所有的superclass的load执行完以后才会执行该类的load,以及class中的load方法是先于category中的load执行的。

关于在category中load方法的执行使用,在iOS黑魔法-Method Swizzling中有很好的运用例子。

运行流程

通过前文对load方法的解释,load方法是会加载所有的superclass后才会加载该类的load,最后加载该类的画category。那么一个工程中有多不同的类那么load方法的执行顺序又是什么样的呢?


测试工程类demo.png

如上图我创建的测试工程中有如下几个类

爸爸儿子也都有了,废话不多说工程跑起来,断点打起来吧~
经过一阵断点测试,工程是这么走的
FatherClass -> SonClass -> FatherView -> SonClass+Load
通过这个顺序,确实是可以得出load方法在执行中,会先执行父类的方法,然后再执行子类的方法,最后执行了category的结论,当然前提是load方法需要在类中实现。但是FatherView的load方法比SonClass+Load中的先执行了,应该是因为FatherView即和其他的类没有继承关系,类型也不相同,在加载的时候是属于同一优先级,异步加载的情况下就比SonClass+Load先执行了。

然后继续测试玩一下,我在测试工程的main方法里打了断点,又在appdelegate的didFinishLaunchingWithOptions方法上打了断点。


测试工程断点图.png

断点也都有了,继续跑一篇我们的工程~
然后就有了这样的执行顺序

FatherClass -> SonClass -> SonClass+Load -> main -> appdelegate

load方法的对应的类只要是引入了,并且该类实现了load方法,在main函数执行之前就会一一执行各类的load方法,load方法的执行优先级还是非常高的,因为执行在main函数之前,所以千万不要在这里初始化OC的对象,因为load执行的时候你不知道你使用的对象是否已经被加载进来,所以无法预知情况。

运用

从上文的运行流程分析中已经了解到,main函数是整个应用运行的入口load方法是在main函数之前执行的,并且只执行一次,load方法既然这么特殊,那么在使用他时肯定还是要注意很多东西。

+ (void)load {
    for (NSInteger i = 0; i < 10000; i ++) {
        NSLog(@"%zd" , i);
    }
}
+ (void)load {
    Method originalFunc = class_getInstanceMethod([self class], @selector(originalFunc));
    Method swizzledFunc = class_getInstanceMethod([self class], @selector(swizzledFunc));
    method_exchangeImplementations(originalFunc, swizzledFunc);
}

在load方法中使用Method Swizzle 是一个常用场景。

此篇博客中参考了其他几个博主的一些知识点,自己做了一些实验证实和总结,全当是加深自己印象的笔记,如果读者有觉得不够详尽或者不够明白的位置,可以参考下面几个相关博客的内容。
👇👇👇👇
相关博客:
NSObject中+(void)load运行流程解析
细说OC中的load和initialize方法
iOS黑魔法-Method Swizzling

最后分享一下家里的小喵~


么么哒.jpeg

吸一口,精神抖擞!

上一篇 下一篇

猜你喜欢

热点阅读