内容
- load
- initialize
- 参考链接
load方法
官方文档说明:
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.
在main
函数之前系统会递归调用,另外需要注意的是:
- 方法线程安全
- 在
runtime
调用+load
方法前后是加了objc_autoreleasePoolPush()
和objc_autoreleasePoolPop()
的 - 按照继承顺序递归调用
- 先本类,后分类
使用场景
Method Swizzle
+ (void)load {
Method originalFunc = class_getInstanceMethod([self class], @selector(originalFunc));
Method swizzledFunc = class_getInstanceMethod([self class], @selector(swizzledFunc));
method_exchangeImplementations(originalFunc, swizzledFunc);
}
initialize
官方文档说明:
Initializes the class before it receives its first message.
The runtime sends initialize to 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.
// In Parent.m
+ (void)initialize {
if (self == [Parent class]) {
NSLog(@"Initialize Parent, caller Class %@", [self class]);
}
}
使用场景
initialize
方法一般用于初始化全局变量或静态变量。
网友评论