In iOS development, it is common to see that the load and initialize class methods are overwritten in a custom class. Before these two methods are understood quite shallow. Today, we will explore the practical functions of the next two methods
The load method
- When the program starts, all classes are loaded into the code area.
- The load method is called when the current class is added to memory, and the entire process is called only once.
- If the class has an inheritance relationship, the load method of the parent class is called first, and then the load method of the child class is called.
- If the class’s class also implements a load method, the load method of the class is called first, and then the load method of the class is called
- When a subclass does not implement a load method, the loaf method of its parent class is not called
2021-10-29 09:13:42.465207+ Person load 2021-10-29 09:13:42.466004+ Person load 2021-10-29 09:13:42.466004+ [Man Load] 2021-10-29 09:13:42.466063+0800 class essence [2693:59888] +[GentleMan load] 2021-10-29 09:13:42.466197+0800 [2693:59888] +[Person(Work) Load] 2021-10-29 09:13:42.466235+0800 [2693:59888] +[Man(Work) Load] 2021-10-29 09:13:42.466269+0800 Class nature [2693:59888] +[GentleMan(Work) Load]Copy the code
Person > Man > GentleMan, so the load methods of Person, Man, and GentleMan are called in sequence. The class load method is called after the class load method is called. The order in which the class load method is called depends on the order in which the class is compiled.
2021-10-29 09:13:42.466197+ [Person(Work) Load] 2021-10-29 09:13:42.466235+0800 class essence [2693:59888] +[Man(Work) Load] 2021-10-29 09:13:42.466269+ [GentleMan(Work) Load]Copy the code
2021-10-29 09:21:59.535424+0800 class essence [2929:67061] +[GentleMan(Work) load] 2021-10-29 09:21:59.535470+0800 [2929:67061] +[Person(Work) Load] 2021-10-29 09:21:59.535510+ [Person(Work) Load]Copy the code
The initialize method
- The Initialize method is called the first time the class is used.
- Initialize methods are block safe, so some simple initialization should be done in initialize methods
- If a subclass implements initialize, the initialize method of the parent class is called first and the initialize method of the subclass is called in the same order as the load method
- If a subclass does not implement initialize, the initialize method of its parent class will be called. If a subclass does not implement initialize, the initialize method of its parent class will be called multiple times.
- If the class overrides the Initialize method, it overrides the class’s own initialize method
- [super Initialize] does not need to be called because it is automatically called by the system. Otherwise, the initialize of the parent class will be executed multiple times. Subclasses are called sequentially from inheritance relationships
- The initialize method is overwritten when multiple categories implement the initialize method, and only one of the class methods is executed.
2021-10-29 09:38:40.921775+ class nature [3416:81482] +[Person initialize] 2021-10-29 09:38:40.922198+ Class nature [3416:81482] +[Man initialize] 2021-10-29 09:38:40.922246+ [3416:81482] +[GentleMan initialize]Copy the code
When GentleMan does not implement the Initialize method
2021-10-29 09:40:18.681950+0800 class essence [3463:82974] +[Person initialize] 2021-10-29 09:40:18.682322+0800 class essence [3463:82974] +[Man initialize] 2021-10-29 09:40:18.682369+ [3463:82974] +[Man initialize]Copy the code
The Person class implements the Initialize method
2021-10-29 09:41:14.920573+ class nature [3491:83855] +[Person(Work) initialize] 2021-10-29 09:41:14.920945+0800 [3491:83855] +[Man initialize] 2021-10-29 09:41:14.920999+Copy the code
conclusion
Initialize is only called once, and the class overrides its own initialize method, so it is better to use load if you need to initialize the class and class separately
2. The Initialize method should not perform complex initialization, but should be limited to simple class native initialization
Prevent the initialize method from being called multiple times if the subclass does not implement the initialize method
4. Official explanation
//In Person.m +(void)initialize { if(self == [Person class]) { NSLog(@"%s",__FUNCTION__); }}Copy the code