● load方法會在加載類的時候就被調(diào)用蛛倦,也就是說iOS應(yīng)用啟動的時候,就會加載所有的類绍申,會調(diào)用每個類的+load方法。
+load方法舉例
-新建父類Person類 并在.m里面實現(xiàn)+load方法
#import <Foundation/Foundation.h>
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里面實現(xiàn)+load方法
#import <Foundation/Foundation.h>
#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類創(chuàng)建Category 并且在.m里面實現(xiàn)+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方法在對類沒有進行任何操作的情況下默認調(diào)用而且在main函數(shù)之前調(diào)用羽戒。
●調(diào)用順序:父類---> 子類---> 分類
●平級調(diào)用順序 (例如又創(chuàng)建了person2類 )這個時候的調(diào)用順序為項目-->TARGETS —>Build Phases -->Compile Sources -加載的順序
+initialize方法舉例
在上述Person類.m中實現(xiàn)+(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
●運行發(fā)現(xiàn)沒有initialize方法調(diào)用
在main函數(shù)中初始化
#import <Foundation/Foundation.h>
#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
●運行發(fā)現(xiàn)initialize方法在init方法前調(diào)用
這時在Person+Category.m中實現(xiàn)initialize方法 并且運行
#import "Person+Category.h"
#import <AppKit/AppKit.h>
@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
●運行發(fā)現(xiàn)Person的方法被Person+Category里的initialize方法替換
這時在Son類中實現(xiàn)+initialize方法 并且在main函數(shù)中初始化并運行
#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 <Foundation/Foundation.h>
#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
●運行發(fā)現(xiàn)Person的方法被Person+Category里的initialize方法替換 無論初始化多少次 Person+Category.m里的方法只會調(diào)用一次(猜測如果沒有分類那么父類的initialize也只會調(diào)用一次下面論證)
這時將main函數(shù)里的父類初始化去掉運行
#import <Foundation/Foundation.h>
#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
●運行發(fā)現(xiàn)初始化子類 父類的分類里的initialize會被調(diào)用(猜測如果沒有分類那么會先調(diào)用父類的initialize )在調(diào)用子類的initialize
好的乘勝追擊 這時將分類里的initialize方法去掉 再次運行
#import "Person+Category.h"
#import <AppKit/AppKit.h>
@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]
●發(fā)現(xiàn)只初始化子類父類的initialize會調(diào)用 父類有分類并實現(xiàn)initialize方法會會把 父類里分方法替換掉 父類不會在調(diào)用 (分類里實現(xiàn)initialize方法會把類里面的initialize方法替換掉)
這時候打開main函數(shù)的父類初始化 子類與父類進行多次初始化 并運行
#import <Foundation/Foundation.h>
#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]
●發(fā)現(xiàn)無論你初始化多少次 initialize方法 有且只會調(diào)用一次
總結(jié)
load
●load的方法編譯器自動調(diào)用 每個類都會調(diào)用 并且在main函數(shù)之前調(diào)用
●每個類的調(diào)用順序跟項目加載的順序有關(guān)具體請看項目-->TARGETS —>Build Phases -->Compile Sources -加載的順序(從上往下調(diào)用)
●繼承關(guān)系并且父類實現(xiàn)分類 順序為 父類 ->子類->分類
initialize
●類初始化的時候調(diào)用 無論類初始化多少次 只會調(diào)用一次(在類第一次初始化之前)
●如果類有分類那么分類的initialize方法會將類本身的initialize方法替換掉將踢匣,(說白了類的initialize方法不會調(diào)用了 分類的initialize方法會調(diào)用) 分類的方法執(zhí)行 有且只調(diào)用一次
●調(diào)用順序 父類的intialize方法執(zhí)行 子類在執(zhí)行 父類-->子類