+load 和 +initialize

APP before starting to perform the main function, the program can execute a lot of code.

The order of execution:

  1. The program depends on the dynamic link library is loaded into memory
  2. All symbols load executable file, the code is compiled runtime parsing of the code symbol
  3. Through all the class
  4. Inheritance hierarchy by calling the load method of load and Class category once.

Comparison + (void) initialize the two methods (void) load +

+load +initialize
Call timing When added runtime Upon receipt of the first message, it may never call
Calling sequence Parent -> subclass -> Classifieds Parent -> subclass
Invocations 1 time The system is executed once, the manual can be called multiple times
The need to explicitly call the parent class implementation no no
If followed to achieve the parent class no Yes
Achieve classification Classes and perform classification To classify covered in class

It calls the initialize method is thread-safe.

load order of execution:

  1. For two library dependencies, it is dependent on the load class of priority calls. But in a library, the calling sequence is uncertain.
  2. load a class method need not specify the [super load], the parent will receive a call, and before the subclass. That is, before the load method execution subclass when the parent class is not loaded will first execute the Load method of the parent class.
  3. Category classification method is executed last,
  4. Before executing the above execution order compile sources of the load.
  5. For a class, there is no way to achieve load will not be called, will not consider the inheritance of the NSObject.

initialize the execution order:

  1. Nature calls the initialize is in 第一次主动使用the current class time.
  2. When the initialize method call is received, the operating environment is basically sound.
  3. During operation of the initialize is to ensure thread-safe.
  4. And load different, even if the child class does not implement the initialize method, will implement the parent class inherited call again. Note that before this, the parent class has been executed once, the same does not need super call.

Same point:

  1. Without considering the developers take the initiative to use, the system will call more than once
  2. 父类在子类之前被调用。
  3. 都是为了应用运行提前创建合适的运行环境

不同点:

  1. load 方法会在加载类的时候就被调用,也就是 ios 应用启动时就会加载所有的类,就会调用每个类的 +load 方法;initialize 方法会在第一次初始化这个类之 前被调用,我们用它来初始化静态变量。
  2. load会在 main() 函数之前调用,+initialize 则在类实例化或调用类方法时调用。load 顺序在 initialize 之前。
  3. 如果子类中没有 +initialize 方法,则会再次调用父类的 +initialize 方法。
  4. 类别会覆盖主类的 +initialize 方法,+load 方法则不会被覆盖。
  5. +initialize 方法的调用看起来会更合理,通常在它里面写代码比在 + load 里写更好,因为它是懒调用的,是有可能完全不被调用的。
  6. 类接收消息时,运行时会先检查 + initialize 有没有被调用过。如果没有,则会在消息被处理前调用。
  7. initialize 最终是通过 objc_msgSend 来执行的,objc_msgSend 会执行一系列方法查找,并且 Category 的方法会覆盖类中的方法;load 是在被添加到 runtime 时开始执行,父类最先执行,然后是子类,最后是 Category。又因为是直接获取函数指针来执行,不会像 objc_msgSend 一样会有方法查找的过程。

Guess you like

Origin www.cnblogs.com/dins/p/12364703.html