Load is different from initialize

1. Call timing

Load is called by Runtime when it loads classes and classes (it only calls once)

Initialize is called the first time a class receives a message, and each class only initialize once. (The initialize method of the parent class may be called multiple times

2. Call method

Load traverses the list of methods and is called directly from the method address;

Initialize is called using objc_msgSend

3. Call sequence

Load method call sequence: call the load method of the class first, compile first, call the load method of the parent class first. Then call the load method of the classification, compile first call first.

Initialize methods are called in order: initialize the parent class first, then initialize the child class (the initialize method of the parent class may be called eventually). If a Category exists and there is an implementation initialize in the Category, the initialize class is called, and neither the class itself nor the precompiled class is called. If the class is not implemented, then the parent class is called (this is the objc_msgSend message mechanism).

Category (category) series

The role of classification

Function: You can extend methods for a class without modifying the original class.

Main usage: to the system’s own class extension methods.

The execution priority of the classification

A. If the method of the class and the classification have the same method, the method of the class will be overwritten and the method of the classification will be invoked first. B. If there are two classes, and they both implement the same method, how do you decide which one executes first? The classification order can be adjusted by using targets,Build Phases, and Complie Source. Note that the execution order is top-down. (Only two classes with the same method name)

The difference between categories and class extensions

(1) In principle, only methods can be added to classes (the reason for adding attributes is to solve the problem of no setter/getter through the Runtime);

(2) Class extension can not only add methods, but also add instance variables (or attributes), which are of type @private by default (scope can only be used in its own class, not subclasses or elsewhere);

③ The compiler will raise an alarm if a method declared in a class extension is not implemented, but the compiler will not raise any alarm if a method declared in a class extension is not implemented. This is because class extensions are added to the class at compile time, whereas categories are added to the class at run time.

(4) A class extension cannot have a separate implementation part (the @implementation part) like a class does. That is, the methods declared by a class extension must rely on the implementation part of the corresponding class.

⑤ Class extension methods defined in.m files are private, while class extension methods defined in.h files (header files) are public. Class extensions are a great way to declare private methods in.m files.