There are three types of initialization classes that inherit:+ (void) Load: + (void) Initialize: -(instancetype) init: + (void) Load: will be automatically invoked (or manually called), as long as the reference runtime will automatically call the class's + (void) Load method, that is, #import "" + (void) Initialize: called automatically (or manually), before the first method of a class is called. It is also stated that runtime's call to + (void) load is not considered the first method of the class. -(instancetype) init: Manual Call requires the Super keyword to invoke the parent class's method, so that the parent class will do the same runtime call + (void) load is not autorelease pool because the program has not established its Autorelease pool when runtime calls + (void) load, So the code that will need to use the Autorelease pool, there will be exceptions. This is very important to note that the objects placed in the + (void) load should be alloc out and cannot be freed using Autorelease.
|
+ (void) load |
+ (void) Initialize |
|
| Timing of execution |
1, the program runs immediately after the execution; 2, the program will be executed only once, 3, if a method only want to execute once, put it here |
1, when the method of the class is called for the first time to execute; 2. It is not invoked when the program is started; |
|
| If you do not define it, use the parent class method |
Whether |
Is |
|
| Definitions in the classification |
All executed, but methods later in the class |
Overrides a method in a class, performing only one |
|
Based on this feature, when creating a tool class, we can use the+ (void) initialize to complete initialization of some objects //--------------------------Test--------------------------------------------------------- -define the Gatest class:#import <Foundation/Foundation.h> @interface gatest: nsobject + (void) gatest;
-(void) test; @endimplementation:#import "GATest.h" @implementation gatest -(Instancetype) init{
If(Self= [super init]) {
nslog (@ " Init ... " }
return self;
}
+ (void) load{
NSLog< Span data-mce-= "" > (@ "load .... }
+ (void) initialize{
nslog (@ "Initialize ..." }+ (void) gatest{
NSLog(@ " called the Gatest class method "); }@end--------------------------------------------------------------------------------- ---#import<Foundation/Foundation.h>
#import"GATest.h"
IntMainIntargcConstChar* argv[]) {
@autoreleasepool{
Insert code here ...
NSLog(@ "Hello, world!" );
[Gatest Gatest];
gatest *gat = [[gatest alloc]init];
[Gat Test];
}
return 0; }Results: ------------------------------------------------------------------------------------
iOS class initialization