一个类的load方法,当类加载到OC运行时环境(内存)中,就会调用一次(一个类只会加载1次)
+(void)load
{
_instance = [[self alloc] init];
NSLog(@"load");
}
一个类的initialize方法,当第一次使用这个类的时候才会调用
+(void)initialize
{
NSLog(@"initialize");
}
load方法和initialize方法的区别
新建一个类DYPerson和它的子类DYStudent。
#import <Foundation/Foundation.h>
@interface DYPerson : NSObject
@end
#import "DYPerson.h"
@implementation DYPerson
+(void)load
{
NSLog(@"DYPerson-->load");
}
+(void)initialize
{
NSLog(@"DYPerson-->initialize");
}
@end
#import "DYPerson.h"
@interface DYStudent : DYPerson
@end
#import "DYStudent.h"
@implementation DYStudent
+(void)load
{
NSLog(@"DYStudent-->load");
}
+(void)initialize
{
NSLog(@"DYStudent-->initialize");
}
@end
在ViewController里面调用DYPerson *person = [[DYPerson alloc] init];则会打印
2017-05-04 00:38:36.422 AnimationArray[6544:2798211] DYPerson-->load
2017-05-04 00:38:36.426 AnimationArray[6544:2798211] DYStudent-->load
2017-05-04 00:38:36.810 AnimationArray[6544:2798211] DYPerson-->initialize
若在ViewController里面调用DYStudent *student = [DYStudent alloc] init];则会打印
2017-05-04 00:47:28.540 AnimationArray[7029:2804754] DYPerson-->load
2017-05-04 00:47:28.543 AnimationArray[7029:2804754] DYStudent-->load
2017-05-04 00:47:28.682 AnimationArray[7029:2804754] DYPerson-->initialize
2017-05-04 00:47:28.682 AnimationArray[7029:2804754] DYStudent-->initialize