Перед тем, как приложение войдет в функцию main (), среда выполнения загрузит все классы и категории.Показывая в качестве следующего кода и выходов консоли, Первичный класс -> категория BB -> категория TT загружается последовательно.
У меня такой вопрос, хотя категория BB и категория TT не были загружены, почему в первичном классе +load
метод, мы можем получить все методы?
исходный код:
static void func() {
uint count;
Method *list = class_copyMethodList([ViewController class], &count);
NSLog(@"count: %d", count);
for (NSInteger i = 0; i < count; i++) {
Method method = list[i];
SEL name = method_getName(method);
IMP imp = method_getImplementation(method);
}
free(list);
}
@implementation ViewController
+ (void)load {
NSLog(@"primary class load");
func();
}
- (void)stub {}
@end
@implementation ViewController (BB)
+ (void)load {
NSLog(@"category (BB) load");
func();
}
- (void)stub {}
@end
@implementation ViewController (TT)
+ (void)load {
NSLog(@"category (TT) load");
func();
}
- (void)stub {}
@end
консольный вывод:
2018-10-08 19:40:46.726720+0800 MethodListTest[25175:1698612] primary class load
2018-10-08 19:40:46.727391+0800 MethodListTest[25175:1698612] count: 3
2018-10-08 19:40:46.727491+0800 MethodListTest[25175:1698612] category (BB) load
2018-10-08 19:40:46.727555+0800 MethodListTest[25175:1698612] count: 3
2018-10-08 19:40:46.727624+0800 MethodListTest[25175:1698612] category (TT) load
2018-10-08 19:40:46.727708+0800 MethodListTest[25175:1698612] count: 3