好用的 UIViewController 生命周期Log输出方案(代码片段为分类.m文件.h文件无属性)

#import "UIViewController+LGFVCLog.h"

@implementation UIViewController (LGFVCLog)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 如果是实例方法:
        Class class = [self class];
        // 如果是类方法
        // Class class = object_getClass((id)self);
        
        // 替换 viewDidLoad
        SEL viewDidLoad = @selector(viewDidLoad);
        SEL lgf_ViewDidLoad = @selector(lgf_ViewDidLoad);
        [self lgf_MethodReplaceWithClass:class selOne:viewDidLoad selTwo:lgf_ViewDidLoad];
        
        // 替换 dealloc
        SEL dealloc = NSSelectorFromString(@"dealloc");
        SEL lgf_Dealloc = @selector(lgf_Dealloc);
        [self lgf_MethodReplaceWithClass:class selOne:dealloc selTwo:lgf_Dealloc];
    });
}

#pragma mark - 运行时方法替换
/**
 @param class 方法所属的类
 @param selOne 被替换的方法
 @param selTwo 替换的方法
 */
+ (void)lgf_MethodReplaceWithClass:(Class)class selOne:(SEL)selOne selTwo:(SEL)selTwo {
    Method met_One = class_getInstanceMethod(class, selOne);
    Method met_Two = class_getInstanceMethod(class, selTwo);
    BOOL didAddMethod =
    class_addMethod(class, selOne, method_getImplementation(met_Two), method_getTypeEncoding(met_Two));
    if (didAddMethod) {
        class_replaceMethod(class, selTwo, method_getImplementation(met_One), method_getTypeEncoding(met_One));
    } else {
        method_exchangeImplementations(met_One, met_Two);
    }
}

- (void)lgf_ViewDidLoad {
    NSLog(@"LGF控制器:%@ --- 已经走 ViewDidLoad", self);
}

- (void)lgf_Dealloc {
    NSLog(@"LGF控制器:%@ --- 已经释放", self);
}

@end

你可能感兴趣的:(好用的 UIViewController 生命周期Log输出方案(代码片段为分类.m文件.h文件无属性))