load 和 initialize 中,都不用调用 super的方法
+ (void)load;
The order of initialization is as follows:
1. All initializers in any framework you link to.
2. All+loadmethods in your image.
3. All C++ static initializers and C/C++__attribute__(constructor)functions in your image.
4. All initializers in frameworks that link to you.
In addition:
A class’s+load method is called after all of its superclasses’+load methods.
A category +load method is called after the class’s own +load method(包括所有子类).
In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.
+ (void)initialize;
Initializes the class before it receives its first message.
The runtime sendsinitializeto each class in a program just before the class, or any class that inherits from it, is sent its first message from within the program.
Superclasses receive this message before their subclasses.
The runtime sends the initialize message to classes in a thread-safe manner.
That is, initialize is run by the first thread to send a message to a class, and any other thread that tries to send a message to that class will block until initialize completes.
The superclass implementation may be called multiple times if subclasses do not implement initialize — the runtime will call the inherited implementation — or if subclasses explicitly call[super initialize].
If you want to protect yourself from being run multiple times, you can structure your implementation along these lines:
+ (void)initialize {
if (self == [ClassName self]) {
// ... do the initialization ...
}
}
Because initialize is called in a blocking manner, it’s important to limit method implementations to the minimum amount of work necessary possible. Specifically, any code that takes locks that might be required by other classes in their initialize methods is liable to lead to deadlocks. Therefore, you should not rely on initialize for complex initialization, and should instead limit it to straightforward, class local initialization.
结论: load,先调父类,后调子类,最后调用分类;initialize,像其他方法一样,分类覆盖原有方法
@implementation superClass
+(void)load
{
NSLog(@"superClass load");
}
+(void)initialize {
if (self == [ViewController self]) {
NSLog(@"xxx");
}
@end
@implementation superClass(ct)
+(void)load
{
NSLog(@"category load");
}
+(void)initialize
{
NSLog(@"category initialize");
}
@end
@implementation subClassA
+(void)load{
NSLog(@"subClassA load");
}
@end
@implementation subClassB
+(void)load
{
NSLog(@"subClassB load");
}
@end
NSLog(@"%@", NSStringFromClass( [self class]));
}
@end
2017-03-12 22:28:17.561 load&initializeTest[4816:135672] superClass load
2017-03-12 22:28:17.564 load&initializeTest[4816:135672] subClassA load
2017-03-12 22:28:17.564 load&initializeTest[4816:135672] subClassB load
2017-03-12 22:28:17.564 load&initializeTest[4816:135672] category load
2017-03-12 22:28:17.647 load&initializeTest[4816:135672] category initialize
2017-03-12 22:28:17.655 load&initializeTest[4816:135672] category initialize
2017-03-12 22:28:17.656 load&initializeTest[4816:135672] category initialize