First, load
When the method is called: All classes are loaded when the main method is not executed, and the load method of all classes is called.
loadThe method is thread-safe, it uses locks, and we should avoid threading blocking in the load method.
Some of the scenarios used in the project:
For example, we want to count all pages (Uiviewcontroller, Uitableviewcontroller) data, can be in the Uiviewcontroller category of the Load method to implement methods Swizzle
@implementation Uiviewcontroller (Basemethod) + (void) load { Method originalviewdidload = Class_getinstancemethod ( [Self class], @selector (viewdidload)); Method swizzledviewdidload = Class_getinstancemethod ([self class], @selector (swizzled_viewdidload)); Method_exchangeimplementations (Originalviewdidload, swizzledviewdidload);}
Again, for example, the _afurlsessiontaskswizzling class in afnetworking overrides the Load method, and in which Swizzleresumeandsuspendmethodforclass is called: Swizzling. Interested can go to see the source code.
Second, initialize
The Initialize method is called when the object is initialized, and the Initialize method is used to allocate memory, and the Init method is to create the object.
Specific to the syntax, is to call the Alloc method will be executed when the initialize.
For example: Bird *birdalloc = [Bird alloc]; This sentence will execute the Initialize method of the bird class.
Bird *bird = [Birdalloc init]; This sentence will execute the bird init method.
The Initialize method is typically used to initialize global variables or static variables, or to handle notifications.
Examples in the project:
For example, the following Sbjson code in the source code, Nscharacterset, Nsmutablearray, etc. cannot be initialized at compile time, can be placed in the initialize to assign value.
1 static nscharacterset *kdecimaldigitcharacterset;2 3 @implementation SBJsonTokeniser4 5 + (void) Initialize {6 Kdecimaldigitcharacterset = [Nscharacterset decimaldigitcharacterset];7}
A lock is also used inside the initialize, so it is thread-safe. But also avoid blocking threads, and don't use locks.
Similar to the Load method, the method of the parent class is called inside the Initialize method, and it does not need to be written out of our display. Unlike the Load method, the method of the parent class is called even if the subclass does not implement the Initialize method.
If we only need the Initialize method of the parent class to execute only once, you can write:
1 + (void) Initialize {2 if (self = = [Bird class]) {3 NSLog (@ "%s%@", __func__, [self class]); 4 }5}
We call Super Init in the Init method, but the load, initialize method does not require us to call the Super method, and calling the Super method is superfluous.
Because runtime automatically invokes the parent class load method initialize , the parent class's method is automatically fired with the subclass, and the call is not required to be displayed. On the other hand, if a method in the parent class uses the self (like the method in the example), the reference is still the class itself, not the parent class.
A table summarized by other people:
|
+ (void) load |
+ (void) initialize |
| Timing of execution |
Execute immediately after the program runs |
Executed when the method of the class is first tuned |
| If you do not define it, do you follow the method of the parent class? |
Whether |
Is |
| Definitions in the category |
All executed, but methods later in the class |
Overrides a method in a class, performing only one
|
The code involved in the article was put on GitHub.
Load and Initialize methods