initialize 和 load 的调用时机
代码1
#import "superClass.h"
@implementation superClass
+(void)load{
NSLog(@"super class load");
}
+(void)initialize{
NSLog(@"super class initialize");
}
@end
#import "subClass.h"
#import "otherClas.h"
@implementation subClass
+(void)load{
NSLog(@"subclass load");
}
+(void)initialize{
NSLog(@"subclass initialize");
}
#import "otherClas.h"
@implementation otherClas
+(void)load{
NSLog(@"other load");
}
+(void)initialize{
NSLog(@"other initialize");
}
@end
@end
三个类superclass , subclass 继承superclass,otherclass单独的一个类,我们只打印,看log
i\InitializeAndLoad[16696:6112402] other load
2018-07-20 17:49:03.687000+0800 i\InitializeAndLoad[16696:6112402] super class load
2018-07-20 17:49:03.687130+0800 i\InitializeAndLoad[16696:6112402] subclass load
可以看到,我们什么代码没有写,去调用任何的类,就是创建了三个类,当我们启动程序的时候,每个方法的load就被调用了,而且我在main函数里面加了个log,load的打印在main之后,这里延伸一个问题,load的顺序可以调整吗?答案是可以的,需要改xcode的配置,在 buildphases-compile sources,里面,可以看见一些头文件,调整文件的顺序就是load的加载顺序。
还有一个问题,当我们把subclass的调用放在super之前呢?是先调用sub的load还是super的load呢?答案是还是会先调用super的load,可得出一个结论,load是先调用super然后调用sub的,可以自己做demo测试.
代码2
#import "subClass.h"
#import "otherClas.h"
@implementation subClass
+(void)load{
NSLog(@"subclass load");
NSLog(@"%@ %s", [self class], __FUNCTION__);
}
+(void)initialize{
NSLog(@"subclass initialize");
NSLog(@"%@ %s", [self class], __FUNCTION__);
// otherClas *other = [otherClas new];
}
@end
看log
2018-07-20 18:01:39.142564+0800 i\InitializeAndLoad[16985:6148339] other load
2018-07-20 18:01:43.061249+0800 i\InitializeAndLoad[16985:6148339] super class load
2018-07-20 18:01:43.061424+0800 i\InitializeAndLoad[16985:6148339] subclass load
2018-07-20 18:01:43.061547+0800 i\InitializeAndLoad[16985:6148339] super class initialize
2018-07-20 18:01:43.061655+0800 i\InitializeAndLoad[16985:6148339] subclass initialize
2018-07-20 18:01:43.061824+0800 i\InitializeAndLoad[16985:6148339] subClass +[subClass initialize]
2018-07-20 18:01:43.061970+0800 i\InitializeAndLoad[16985:6148339] subClass +[subClass load]
2018-07-20 18:01:43.062160+0800 i\InitializeAndLoad[16985:6148339] main
从log上可以看出,在subclass开始调用load到load调用完成,中间加了initialize的调用,这是因为,我们在subclas的load方法里面写了SLog(@"subclass load");
NSLog(@"%@ %s", [self class], __FUNCTION__);
这行代码,这行代码里面写了,[self class]
,所以就相当于调用了自己的类的某个方法,这时候,会首先去 调用super的initialize方法,然后调用自己的initialize方法,等initialize方法调用完成之后,这时候load结束,不是说这两个方法一定在load之中,是因为我们在load里面写了代码,所以可以看出,initialize方法的调用时机是,激活了类的某个方法就会激活initialize方法,
代码3
#import "subClass.h"
#import "otherClas.h"
@implementation subClass
+(void)load{
NSLog(@"subclass load");
NSLog(@"%@ %s", [self class], __FUNCTION__);
}
+(void)initialize{
NSLog(@"subclass initialize");
NSLog(@"%@ %s", [self class], __FUNCTION__);
otherClas *other = [otherClas new];
}
@end
log
2018-07-20 18:07:25.478296+0800 i\InitializeAndLoad[17093:6164209] other load
2018-07-20 18:07:25.479142+0800 i\InitializeAndLoad[17093:6164209] super class load
2018-07-20 18:07:25.479295+0800 i\InitializeAndLoad[17093:6164209] subclass load
2018-07-20 18:07:25.479435+0800 i\InitializeAndLoad[17093:6164209] super class initialize
2018-07-20 18:07:25.479548+0800 i\InitializeAndLoad[17093:6164209] subclass initialize
2018-07-20 18:07:25.479993+0800 i\InitializeAndLoad[17093:6164209] subClass +[subClass initialize]
2018-07-20 18:07:25.480129+0800 i\InitializeAndLoad[17093:6164209] other initialize
2018-07-20 18:07:25.480240+0800 i\InitializeAndLoad[17093:6164209] subClass +[subClass load]
2018-07-20 18:07:25.480397+0800 i\InitializeAndLoad[17093:6164209] main
从log可以看出激活了otherclass的initialize方法,并且接在load之间。