initialize和load方法区别

2018年3月12日
initialize和load方法区别

一.相同点
1.两个方法都会自动调用

二.不同点
1.调用时机不同
1.1 load方法
程序启动时,会加加载所有累的load方法。而且执行时间早于main函数,没有调用都会执行

测试代码:

#import 
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
    
    NSLog(@"%s",__func__);
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
#import 
@interface HuPerson : NSObject
@end


#import "HuPerson.h"
@implementation HuPerson
+ (void)load{
    NSLog(@"%s",__func__);
}
+ (void)initialize{
    NSLog(@"%s",__func__);
}
- (instancetype)init{
    if (self = [super init]) {
        NSLog(@"%s",__func__);
    }
    return self;
}
@end
#import 
@interface HuMale : NSObject
@end


#import "HuMale.h"
@implementation HuMale
+ (void)load{
    NSLog(@"%s",__func__);
}
+ (void)initialize{
    NSLog(@"%s",__func__);
}
- (instancetype)init{
    if (self = [super init]) {
        NSLog(@"%s",__func__);
    }
    return self;
}
@end

测试日志:

2018-03-12 16:31:09.291815+0800 HuFileShareWithItunesDemo[27322:1230782] +[HuPerson load]
2018-03-12 16:31:09.292947+0800 HuFileShareWithItunesDemo[27322:1230782] +[HuMale load]
2018-03-12 16:31:09.293232+0800 HuFileShareWithItunesDemo[27322:1230782] main

1.2 initialize
使用的时候会调用,而且只有第一次创建该类的时候会调用,调用次序快于init

1.3 init
使用的时候会调用,每次使用都会调用

1.2和1.3测试代码

#import "ViewController.h"
#import "HuPerson.h"
#import "HuMale.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    HuPerson *aP = [HuPerson new];
    HuPerson *bP = [HuPerson new];
    
    HuMale *aM = [HuMale new];
    HuMale *bM = [HuMale new];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1.2和1.3测试日志

2018-03-12 16:40:42.060036+0800 HuFileShareWithItunesDemo[27517:1268357] +[HuPerson load]
2018-03-12 16:40:42.061337+0800 HuFileShareWithItunesDemo[27517:1268357] +[HuMale load]
2018-03-12 16:40:42.061866+0800 HuFileShareWithItunesDemo[27517:1268357] main
2018-03-12 16:40:42.209059+0800 HuFileShareWithItunesDemo[27517:1268357] +[HuPerson initialize]
2018-03-12 16:40:42.209319+0800 HuFileShareWithItunesDemo[27517:1268357] -[HuPerson init]
2018-03-12 16:40:42.209489+0800 HuFileShareWithItunesDemo[27517:1268357] -[HuPerson init]
2018-03-12 16:40:42.209679+0800 HuFileShareWithItunesDemo[27517:1268357] +[HuMale initialize]
2018-03-12 16:40:42.209816+0800 HuFileShareWithItunesDemo[27517:1268357] -[HuMale init]
2018-03-12 16:40:42.210022+0800 HuFileShareWithItunesDemo[27517:1268357] -[HuMale init]

2.继承后调用关系,默认用的是之类的, 子类没实现load,也不会调用父类的load函数
2.1测试代码

#import "HuMale.h"
@interface HuBoy : HuMale
@end

#import "HuBoy.h"
@implementation HuBoy
+ (void)load{
    NSLog(@"%s",__func__);
}
+ (void)initialize{
    NSLog(@"%s",__func__);
}
- (instancetype)init{
    if (self = [super init]) {
        NSLog(@"%s",__func__);
    }
    return self;
}
@end
#import "ViewController.h"
#import "HuPerson.h"
#import "HuMale.h"
#import "HuBoy.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    HuPerson *aP = [HuPerson new];
    HuPerson *bP = [HuPerson new];
    
    HuMale *aM = [HuMale new];
    HuMale *bM = [HuMale new];
    
    HuBoy *aB = [HuBoy new];
    HuBoy *bB = [HuBoy new];
    
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2.2测试日志

2018-03-12 16:55:49.477013+0800 HuFileShareWithItunesDemo[27821:1336086] +[HuPerson load]
2018-03-12 16:55:49.477789+0800 HuFileShareWithItunesDemo[27821:1336086] +[HuMale load]
2018-03-12 16:55:49.477974+0800 HuFileShareWithItunesDemo[27821:1336086] +[HuBoy load]
2018-03-12 16:55:49.478149+0800 HuFileShareWithItunesDemo[27821:1336086] main
2018-03-12 16:55:49.614960+0800 HuFileShareWithItunesDemo[27821:1336086] +[HuPerson initialize]
2018-03-12 16:55:49.615074+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuPerson init]
2018-03-12 16:55:49.615179+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuPerson init]
2018-03-12 16:55:49.615278+0800 HuFileShareWithItunesDemo[27821:1336086] +[HuMale initialize]
2018-03-12 16:55:49.615388+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuMale init]
2018-03-12 16:55:49.615491+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuMale init]
2018-03-12 16:55:49.615605+0800 HuFileShareWithItunesDemo[27821:1336086] +[HuBoy initialize]
2018-03-12 16:55:49.615922+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuMale init]
2018-03-12 16:55:49.616037+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuBoy init]
2018-03-12 16:55:49.616145+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuMale init]
2018-03-12 16:55:49.616259+0800 HuFileShareWithItunesDemo[27821:1336086] -[HuBoy init]

3.所以,load方法优先级最高,在main方法前执行,initialize类似于懒加载

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(initialize和load方法区别)