+ load and + initialize

+ (void)load;

The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.

+load 函数是由runtime动态加载并且静态链接的,只有在自定义类中重写了才会响应load信息。+load函数的执行时机早于main函数的。在实际的项目开发中运用:

  • 初始化一些全局资源
  • 单例模式
  • 精简

- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions

1
2
3
4
5
6
7
8
9
+ (void)load
{
@autoreleasepool {
__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
//TODO you want add into the didFinishLaunchingWithOptions method
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}];
}
}

+(void)initialize;

>
initialize it is invoked only once per class. If you want to perform independent initialization for the class and for categories of the class, you should implement load methods.

Tips

Unfortunately, a load class method implemented in Swift is never called by the runtime, rendering that recommendation an impossibility.

引用