OC 是一门动态的语言, runtime 的机制给开发者提供了许多新的可能, 在运行时, 可以动态为一个类添加方法和属性.
####首先说一下 objc_msgSend 执行方法
现在我们有一个 Doctor 类:
1 2 3 4 5 6 7
| // Doctor.m @implementation Doctor - (void)sayhello { NSLog(@"hello"); } @end
|
我们都知道不在.h 里面申明, .只在 .m 中实现的方法, 就相当于私有方法. 但并非绝对的私有.
1 2 3 4 5
| Doctor *doctor = [[Doctor alloc] init]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" [doctor performSelector:NSSelectorFromString(@"sayhello") withObject:nil]; #pragma clang diagnostic pop
|
使用 perfromSelector 就可以直接掉用知道名字的方法.
perfromSelector相当于:
1 2 3 4
| Doctor *doctor = [[Doctor alloc] init]; SEL sel = NSSelectorFromString(@"sayhello"); void (*sendMsg)(id, SEL) = (void (*)(id, SEL))objc_msgSend; sendMsg(doctor, sel);
|
runtime 中使用 objc_msgSend 来执行所有的方法.
####class_addMethod 动态添加方法
1 2 3 4 5 6 7 8 9 10
| SEL sel = NSSelectorFromString(@"sayhello"); // 获取当前类 method Method method = class_getInstanceMethod([self class], NSSelectorFromString(@"sayhello")); // 让 Doctor 指向当前类的 method class_addMethod([doctor class], sel, method_getImplementation(method), method_getTypeEncoding(method)); //当前类声明一个方法 - (void)sayhello { NSLog(@"hello"); }
|
Method Swizzling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @implementation UIViewController (Tracking) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; SEL originalSelector = @selector(viewWillAppear:); SEL swizzledSelector = @selector(xxx_viewWillAppear:); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); // 将 originalMethod 指向 xxx_viewWillAppear: 的实现. BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) { // 将 swizzledMethod 指向 viewWillAppear: 的实现. class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } }); } #pragma mark - Method Swizzling - (void)xxx_viewWillAppear:(BOOL)animated { [self xxx_viewWillAppear:animated]; // 这里实际执行的是 viewWillAppear: }
|
通过调换两个方法的实现, 来实现, 捕捉原始方法的执行. 当需要捕捉某个方法执行, 一种方法是通过继承来实现, 另一种方法通过 Method Swizzling, 当有很多不同形态的子类存在的时候, 继承还是需要写很多重复代码.
首先说一下, 当通过 Method Swizzling 交换方法之后, 所有这个类的子类走这个交换过的方法都会, 在当前的实现中捕捉到, 所以, 在这个方法中的 self 是会发生变化的, 可能是任意一个 UIViewController 的子类.
即:
1 2 3 4
| // 所有 UIViewController 的子类和本身, 在走 viewWillAppear 之前都会走下面的方法 - (void)xxx_viewWillAppear:(BOOL)animated { [self xxx_viewWillAppear:animated]; // 这里实际执行的是 viewWillAppear: }
|
在 @sunny
的 xib的动态桥接
中, 他捕捉了所有的 UIView 子类的 awakeAfterUsingCoder
方法, 并通过判断是否遵守协议来判断是否要进行处理, 所以只有在 UIView 子类种, 遵守 XXNibBridge
协议的子类才会被动态加载, 这是 Method Swizzling 一个很好的案例.
1 2 3 4 5 6 7 8
| // 交换后的方法 - (id)hackedAwakeAfterUsingCoder:(NSCoder *)decoder { if ([self.class conformsToProtocol:@protocol(XXNibBridge)] && ((UIView *)self).subviews.count == 0) { // "self" is placeholder view for this moment, replace it. return [XXNibBridgeImplementation instantiateRealViewFromPlaceholder:(UIView *)self]; } return self; }
|
Method Swizzling @Mattt Thompson
xib的动态桥接 @sunny