为什么这个 UIViewController 方法调配不起作用?

why this UIViewController method swizzling doesn't work?

我正在尝试 The NSHipster Fake Book 的代码示例来调整 UIViewController 的 viewWillAppear: 方法。但它似乎无法正常工作,因为 xxx_viewWillAppear: 方法从未被调用过。我只是不知道为什么。请帮助我,谢谢。

#import "ViewController.h"
#import <objc/runtime.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

@end

@implementation UIViewController (Tracking)
+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls = object_getClass(self);
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(xxx_viewWillAppear:);
        Method originalMethod = class_getInstanceMethod(cls, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
        BOOL addMethod = class_addMethod(cls, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (addMethod) {
            class_replaceMethod(cls, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }
        else{
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

-(void)xxx_viewWillAppear:(BOOL)animated
{
    [self xxx_viewWillAppear:animated];
    NSLog(@"viewWillAppear: %@",self);
}
@end

由于load是一个class方法,Class cls = object_getClass(self);中的self指的是metaclass 的 UIViewController,而不是您想要的实际 class (UIViewController)。

如果将其更改为 Class cls = [self class];,则 cls 将成为 UIViewController class 本身,它应该可以工作。