什么是method-swizzling? 简单来说就是修改两个选择器的方法实现。 代码实现 123456789 #import <Foundation/Foundation.h>@interface RuntimeObject : NSObject- (void)test;- (void)otherTest;@end 123456789101112131415161718192021 + (void)load{ Method test = class_getInstanceMethod(self, @selector(test)); Method otherTest = class_getInstanceMethod(self, @selector(otherTest)); method_exchangeImplementations(test, otherTest);}- (void)test { NSLog(@"test");}- (void)otherTest{ //这个self 实际调用test [self otherTest]; NSLog(@"otherTest");}@end 赞微海报分享
近期评论