iOS load方法和initialize方法的异同

● load方法会在加载类的时候就被调用,也就是说iOS应用启动的时候,就会加载所有的类,会调用每个类的+load方法。

+load方法举例

-新建父类Person类 并在.m里面实现+load方法

#import 

NS_ASSUME_NONNULL_BEGIN

@interface Person : NSObject

@end

NS_ASSUME_NONNULL_END
#import "Person.h"

@implementation Person
+(void)load
{
    NSLog(@"%s",__func__);
}
@end

继承Person类新建子类Son类 并在.m里面实现+load方法

#import 
#import "Person.h"

NS_ASSUME_NONNULL_BEGIN

@interface Son : Person

@end

NS_ASSUME_NONNULL_END


#import "Son.h"

@implementation Son
+(void)load
{
    NSLog(@"%s",__func__);
}
@end

给父类Person类创建Category 并且在.m里面实现+load方法

#import "Person.h"

NS_ASSUME_NONNULL_BEGIN

@interface Person (Category)

@end

NS_ASSUME_NONNULL_END

#import "Person+Category.h"

@implementation Person (Category)
+(void)load
{
    NSLog(@"%s",__func__);
}
@end

运行打印

2020-06-16 09:41:35.366135+0800 load+initialize[1673:96489] +[Person load]
2020-06-16 09:41:35.366666+0800 load+initialize[1673:96489] +[Son load]
2020-06-16 09:41:35.366721+0800 load+initialize[1673:96489] +[Person(Category) load]
2020-06-16 09:41:35.366845+0800 load+initialize[1673:96489] main, Hello World!

●实操证明+load方法在对类没有进行任何操作的情况下默认调用而且在main函数之前调用。
●调用顺序:父类---> 子类---> 分类
●平级调用顺序 (例如又创建了person2类 )这个时候的调用顺序为项目-->TARGETS —>Build Phases -->Compile Sources -加载的顺序

+initialize方法举例

在上述Person类.m中实现+(void)initialize方法

#import "Person.h"

@implementation Person
+(void)load
{
    NSLog(@"%s",__func__);
}

+(void)initialize
{
    NSLog(@"%s",__func__);
}

- (instancetype)init
{
    self = [super init];
      if (self) {
             NSLog(@"%s",__func__);
      }
      return self;
}

@end

●运行发现没有initialize方法调用

在main函数中初始化

#import 
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"%s, Hello World!",__func__);

       Person * person = [[Person alloc]init];

    }
    return 0;
}

------------------------load-----------------------------------------
2020-06-16 11:50:52.431490+0800 load+initialize[2080:126579] +[Person load]
2020-06-16 11:50:52.432133+0800 load+initialize[2080:126579] +[Son load]
2020-06-16 11:50:52.432369+0800 load+initialize[2080:126579] +[Person(Category) load]
2020-06-16 11:50:52.432614+0800 load+initialize[2080:126579] main, Hello World!
------------------------initialize----------------------------------------
2020-06-16 11:50:52.432687+0800 load+initialize[2080:126579] +[Person initialize]
2020-06-16 11:50:52.432841+0800 load+initialize[2080:126579] -[Person init]
Program ended with exit code: 0

●运行发现initialize方法在init方法前调用

这时在Person+Category.m中实现initialize方法 并且运行

#import "Person+Category.h"

#import 

@implementation Person (Category)
+(void)load
{
    NSLog(@"%s",__func__);
}

+(void)initialize
{
    NSLog(@"%s",__func__);
}
@end

------------------------load-----------------------------------------
2020-06-16 12:00:33.317059+0800 load+initialize[2128:128769] +[Person load]
2020-06-16 12:00:33.318109+0800 load+initialize[2128:128769] +[Son load]
2020-06-16 12:00:33.318562+0800 load+initialize[2128:128769] +[Person(Category) load]
2020-06-16 12:00:33.318907+0800 load+initialize[2128:128769] main, Hello World!
------------------------initialize----------------------------------------
2020-06-16 12:00:33.319210+0800 load+initialize[2128:128769] +[Person(Category) initialize]
2020-06-16 12:00:33.319515+0800 load+initialize[2128:128769] -[Person init]
Program ended with exit code: 0

●运行发现Person的方法被Person+Category里的initialize方法替换

这时在Son类中实现+initialize方法 并且在main函数中初始化并运行

#import "Son.h"

@implementation Son
+(void)load
{
    NSLog(@"%s",__func__);
}

+(void)initialize
{
    NSLog(@"%s",__func__);
}
- (instancetype)init
{
    self = [super init];
    if (self) {
         NSLog(@"%s",__func__);
    }
    return self;
}

@end

#import 
#import "Person.h"
#import "Son.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"%s, Hello World!",__func__);

            Person * person = [[Person alloc]init];

            Son * son = [[Son alloc]init];

    }
    return 0;
}

------------------------load-----------------------------------------
2020-06-16 12:10:01.456182+0800 load+initialize[2362:141389] +[Person load]
2020-06-16 12:10:01.456668+0800 load+initialize[2362:141389] +[Son load]
2020-06-16 12:10:01.456877+0800 load+initialize[2362:141389] +[Person(Category) load]
2020-06-16 12:10:01.457197+0800 load+initialize[2362:141389] main, Hello World!
------------------------initialize----------------------------------------
2020-06-16 12:10:01.457270+0800 load+initialize[2362:141389] +[Person(Category) initialize]
2020-06-16 12:10:01.457411+0800 load+initialize[2362:141389] -[Person init]
2020-06-16 12:10:01.457516+0800 load+initialize[2362:141389] +[Son initialize]
2020-06-16 12:10:01.457632+0800 load+initialize[2362:141389] -[Person init]
2020-06-16 12:10:01.457724+0800 load+initialize[2362:141389] -[Son init]
Program ended with exit code: 0

●运行发现Person的方法被Person+Category里的initialize方法替换 无论初始化多少次 Person+Category.m里的方法只会调用一次(猜测如果没有分类那么父类的initialize也只会调用一次下面论证)

这时将main函数里的父类初始化去掉运行

#import 
#import "Person.h"
#import "Son.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"%s, Hello World!",__func__);

            ///////Person * person = [[Person alloc]init];
            Son * son = [[Son alloc]init];

    }
    return 0;
}

------------------------load-----------------------------------------
2020-06-16 12:14:05.053879+0800 load+initialize[2414:144467] +[Person load]
2020-06-16 12:14:05.054447+0800 load+initialize[2414:144467] +[Son load]
2020-06-16 12:14:05.054611+0800 load+initialize[2414:144467] +[Person(Category) load]
2020-06-16 12:14:05.054742+0800 load+initialize[2414:144467] main, Hello World!
------------------------initialize----------------------------------------
2020-06-16 12:14:05.054797+0800 load+initialize[2414:144467] +[Person(Category) initialize]
2020-06-16 12:14:05.054842+0800 load+initialize[2414:144467] +[Son initialize]
2020-06-16 12:14:05.054889+0800 load+initialize[2414:144467] -[Person init]
2020-06-16 12:14:05.054929+0800 load+initialize[2414:144467] -[Son init]
Program ended with exit code: 0

●运行发现初始化子类 父类的分类里的initialize会被调用(猜测如果没有分类那么会先调用父类的initialize )在调用子类的initialize

好的乘胜追击 这时将分类里的initialize方法去掉 再次运行

#import "Person+Category.h"

#import 

@implementation Person (Category)
+(void)load
{
    NSLog(@"%s",__func__);
}

//
//+(void)initialize
//{
//    NSLog(@"%s",__func__);
//}
@end

------------------------load----------------------------------------
2020-06-16 12:18:06.368976+0800 load+initialize[2484:146913] +[Person load]
2020-06-16 12:18:06.369644+0800 load+initialize[2484:146913] +[Son load]
2020-06-16 12:18:06.369725+0800 load+initialize[2484:146913] +[Person(Category) load]
2020-06-16 12:18:06.369850+0800 load+initialize[2484:146913] main, Hello World!

------------------------initialize----------------------------------------
2020-06-16 12:18:06.369904+0800 load+initialize[2484:146913] +[Person initialize]
2020-06-16 12:18:06.369946+0800 load+initialize[2484:146913] +[Son initialize]
2020-06-16 12:18:06.369993+0800 load+initialize[2484:146913] -[Person init]
2020-06-16 12:18:06.370032+0800 load+initialize[2484:146913] -[Son init]

●发现只初始化子类父类的initialize会调用 父类有分类并实现initialize方法会会把 父类里分方法替换掉 父类不会在调用 (分类里实现initialize方法会把类里面的initialize方法替换掉)

这时候打开main函数的父类初始化 子类与父类进行多次初始化 并运行

#import 
#import "Person.h"
#import "Son.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"%s, Hello World!",__func__);

         Person * person = [[Person alloc]init];
            Person * person1 = [[Person alloc]init];
            Person * person2 = [[Person alloc]init];
            Person * person3 = [[Person alloc]init];

        Son * son = [[Son alloc]init];
               Son * son1 = [[Son alloc]init];
               Son * son2 = [[Son alloc]init];
               Son * son3 = [[Son alloc]init];

    }
    return 0;
}

------------------------load-----------------------------------------
2020-06-16 12:20:08.090877+0800 load+initialize[2697:156107] +[Person load]
2020-06-16 12:20:08.091536+0800 load+initialize[2697:156107] +[Son load]
2020-06-16 12:20:08.091603+0800 load+initialize[2697:156107] +[Person(Category) load]
2020-06-16 12:20:08.091728+0800 load+initialize[2697:156107] main, Hello World!
------------------------initialize----------------------------------------
2020-06-16 12:20:08.091937+0800 load+initialize[2697:156107] +[Person initialize]
2020-06-16 12:20:08.092105+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.092155+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.092196+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.092236+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.092276+0800 load+initialize[2697:156107] +[Son initialize]
2020-06-16 12:20:08.092318+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.092371+0800 load+initialize[2697:156107] -[Son init]
2020-06-16 12:20:08.092411+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.092448+0800 load+initialize[2697:156107] -[Son init]
2020-06-16 12:20:08.092487+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.094391+0800 load+initialize[2697:156107] -[Son init]
2020-06-16 12:20:08.094461+0800 load+initialize[2697:156107] -[Person init]
2020-06-16 12:20:08.094511+0800 load+initialize[2697:156107] -[Son init]

●发现无论你初始化多少次 initialize方法 有且只会调用一次

总结

load

●load的方法编译器自动调用 每个类都会调用 并且在main函数之前调用
●每个类的调用顺序跟项目加载的顺序有关具体请看项目-->TARGETS —>Build Phases -->Compile Sources -加载的顺序(从上往下调用)
●继承关系并且父类实现分类 顺序为 父类 ->子类->分类

initialize

●类初始化的时候调用 无论类初始化多少次 只会调用一次(在类第一次初始化之前)
●如果类有分类那么分类的initialize方法会将类本身的initialize方法替换掉将,(说白了类的initialize方法不会调用了 分类的initialize方法会调用) 分类的方法执行 有且只调用一次
●调用顺序 父类的intialize方法执行 子类在执行 父类-->子类

iOS load方法和initialize方法的异同_第1张图片
image.png

你可能感兴趣的:(iOS load方法和initialize方法的异同)