其实就是动态添加方法:
1 2
| RuntimeTest *test = [[RuntimeTest alloc] initWithLog:@"123"]; [test performSelector:@selector(printLog1:log2:) withObject:@"One" withObject:@"Two"]; // 动态添加方法
|
1 2 3 4 5 6 7 8 9 10
| + (BOOL)resolveInstanceMethod:(SEL)sel { if (sel == @selector(printLog1:log2:)) { class_addMethod(self, @selector(printLog1:log2:), (IMP)printLog, "v@:@@"); } return [super resolveInstanceMethod:sel]; }
void printLog(id self, SEL _cmd, NSString *log1, NSString *log2) { //TODO }
|
Demo 地址
https://github.com/GhostClock/Runtime-Demo