iOS|iOS Runtime交换对象方法和类方法探讨

在项目中,经常用到runtime交换方法的地方,所以项目里前人给NSObject打了一个分类。代码如下(方法为什么要先addMethod 做判断,是因为我们要替换的方法有可能并没有在这个类中被实现,而是在他的父类中实现的,这个时候originSEL获取到的方法就是他父类中的方法,所以我们要在当前的类中添加一个originSEL方法,具体参考http://www.jianshu.com/p/a6b675f4d073)

@implementation NSObject (Runtime)+ (BOOL)swizzleMethod:(Class)class orgSel:(SEL)origSel swizzSel:(SEL)altSel { Method origMethod = class_getInstanceMethod(class, origSel); Method altMethod = class_getInstanceMethod(class, altSel); if (!origMethod || !altMethod) { return NO; } BOOL didAddMethod = class_addMethod(class,origSel, method_getImplementation(altMethod), method_getTypeEncoding(altMethod)); if (didAddMethod) { class_replaceMethod(class,altSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { method_exchangeImplementations(origMethod, altMethod); }return YES; }

使用时就在相应的类的load方法类进行方法替换
@implementation NSArray (SafeArray)+(void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // 交换对象方法 [self swizzleMethod:objc_getClass("__NSArrayI") orgSel:@selector(objectAtIndex:) swizzSel:@selector(safe_objectAtIndex:)]; // 交换类方法 [self swizzleMethod:object_getClass((id)self) orgSel:@selector(arrayWithObjects:count:) swizzSel:@selector(safe_arrayWithObjects:count:]; }); }

针对于上述数组类型的交换方法,交换对象方法和类方法使用了objc_getClass,object_getClass。 先再了解一下object_getClass,objc_getClass
object_getClass :
当obj为实例变量时,object_getClass(obj)与[obj class]输出结果一直,均获得isa指针,即指向类对象的指针。
当obj为类对象时,object_getClass(obj)返回类对象中的isa指针,即指向元类对象的指针;[obj class]返回的则是其本身
objc_getClass:
返回字符串对应的对象。和 NSClassFromString一致
为什么使用objc_getClass, 因为NSArray是类簇, 关于objectAtIndex,objectAtIndexedSubscript 方法的实现其实是_NSArrayI实现的,所以使用objc_getClass("__NSArrayI") 获取实际的类。不是类簇的类,是可以直接传入 self 或者[self class] 进行交换对象方法的。 ☆☆而此处要探讨的是为什么交换类方法可以使用object_getClass获取Class,也就是[self swizzleMethod:object_getClass((id)self) orgSe...]的方式交换成功☆☆
我们再回到交换方法的实现+ (BOOL)swizzleMethod:(Class)class orgSel:(SEL)origSel swizzSel:(SEL)altSel内部
如果对runtime不是很了解的时候,看到这里之前就有几个疑问。
1.分类里取method都是取对象方法class_getInstanceMethod取的,但是如果取类方法理应该是用class_getClassMethod这个api。而上述分类不这样写也能成功交换到类方法,原因是什么
2. 类方法交换时要这样使用object_getClass("xxx")取Class的原因
【iOS|iOS Runtime交换对象方法和类方法探讨】为了便于测试,自己定义了一个类 以及分类,仿照NSArray 的交换写法
#import @interface TestModel : NSObject + (void)classMethod; - (void)instanceMethod; @end#import "TestModel.h" @implementation TestModel + (void)classMethod { NSLog(@"调用了原本的类方法"); } - (void)instanceMethod { NSLog(@"调用了原本的对象方法"); } @end

#import "TestModel+swizzeTest.h" #import "NSObject+SwizzleMethod.h" @implementation TestModel (swizzeTest)+ (void)load { // 使用这种写法可以 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self swizzleMethod:self orgSel:@selector(instanceMethod) swizzSel:@selector(swizzeInstanceMehtod)]; [self swizzleMethod:object_getClass(self) orgSel:@selector(classMethod) swizzSel:@selector(swizzeClassMehtod)]; }); } - (void)swizzeInstanceMehtod { NSLog(@"调用了交换后的对象方法"); } + (void)swizzeClassMehtod { NSLog(@"调用了交换后的类方法"); }@end

TestModel *model = [TestModel new]; [model instanceMethod]; [TestModel classMethod];

输出:

iOS|iOS Runtime交换对象方法和类方法探讨
文章图片
输出 结果可以看出交换成功,继续深入
深入一下runtime(http://www.jianshu.com/p/54c190542aa8)
再自己瞎改瞎测试一下;
1.首先
NSObject的分类方法还是如上一样,如果不使用object_getClass("xxx")方式,那么交换类方法的代码改成
[self swizzleMethod:self orgSel:@selector(classMethod) swizzSel:@selector(swizzeClassMehtod)]; <----改动

经测试,果不其然交换类方法是交换失败的,class_getInstanceMethod取出的为origMethodaltMethod 为nil。证实class_getInstanceMethod确实无法取出类方法的。
再尝试再把分类里的代码class_getInstanceMethod改成class_getClassMethod
+ (BOOL)swizzleMethod:(Class)class orgSel:(SEL)origSel swizzSel:(SEL)altSel { Method origMethod = class_getClassMethod(class, origSel); <----改动 Method altMethod = class_getClassMethod(class, altSel); <----改动 if (!origMethod || !altMethod) { return NO; } BOOL didAddMethod = class_addMethod(class,origSel, method_getImplementation(altMethod), method_getTypeEncoding(altMethod)); if (didAddMethod) { // 方法已经添加 class_replaceMethod(class,altSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { method_exchangeImplementations(origMethod, altMethod); }return YES; }

照这样写,origMethodaltMethod 不为空,但是一测试还是没有交换成功类方法。

iOS|iOS Runtime交换对象方法和类方法探讨
文章图片
image.png
每次都走了if里面的逻辑, 反而如果不进行didAddMethod判断直接使用method_exchangeImplementations交换就可以成功,所以怀疑是添加类方法class_addMethod有问题。
再来读下资料: 创建对象的类本身也是对象,称为类对象,类对象中存放的是描述实例相关的信息,例如实例的成员变量,实例方法。
类对象里的isa指针指向Subclass(meta),Subclass(meta)也是一个对象,是元类对象,元类对象中存放的是描述类相关的信息,例如类方法

读上面一段解释,我们可以大胆猜想,给对象加实例方法使用class_addMethod(xx),xx传入的是对象的类。 那使用给类添加类方法,应该给类的类(也就是元类metaClass)进行添加。所以尝试对方法进行修改
Method origMethod = class_getClassMethod(class, origSel); Method altMethod = class_getClassMethod(class, altSel); if (!origMethod || !altMethod) { return NO; } //使用object_getClass(class) 取得元类 BOOL didAddMethod = class_addMethod(object_getClass(class),origSel, <----改动 method_getImplementation(altMethod), method_getTypeEncoding(altMethod)); if (didAddMethod) { class_replaceMethod(class,altSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { method_exchangeImplementations(origMethod, altMethod); }return YES;

这样写,交换成功
通过几步的测试,我们可以就证实上面的猜想(给类添加类方法,应该给类的类(也就是元类metaClass)以及解开关于文中刚开始的疑惑
1.分类里取method都是取对象方法class_getInstanceMethod取的,但是如果取类方法应该是用class_getClassMethod这个api。而上述分类为什么能交换到类方法?
因为通过object_getClass获取到的是类的元类,因为类也是对象,类方法对于元类来说就是是它的对象方法,当class为元类时,使用class_getInstanceMethod是可以取出方法的,取出的为类方法。
2. 为什么类方法交换时要这样使用object_getClass("xxx")调用 swizzleMethod
针对于文中最开始的的交换方法实现,对于交换实例方法,直接传入对象的class即可,而对于交换类方法,则需传入类的元类,object_getClass()取出的就是元类,才能进行交换。
总结 1.如果使用 object_getClass 得到元类来进行交换类方法的话,交换类方法和对象只需要共用一个交换对象方法的方法就行。
[self swizzleMethod:self orgSel:@selector(instanceMethod) swizzSel:@selector(swizzeInstanceMehtod)]; [self swizzleMethod:object_getClass(self) orgSel:@selector(classMethod) swizzSel:@selector(swizzeClassMehtod)];

2.如果直接采用类进行调用的话,那么方法就应该分为交换对象方法和交换类方法。
[self swizzleMethod:self orgSel:@selector(instanceMethod) swizzSel:@selector(swizzeInstanceMehtod)]; [self swizzleClassMethod:self orgSel:@selector(classMethod) swizzSel:@selector(swizzeClassMehtod)];

+ (BOOL)swizzleMethod:(Class)class orgSel:(SEL)origSel swizzSel:(SEL)altSel { Method origMethod = class_getInstanceMethod(class, origSel); Method altMethod = class_getInstanceMethod(class, altSel); if (!origMethod || !altMethod) { return NO; } BOOL didAddMethod = class_addMethod(class,origSel, method_getImplementation(altMethod), method_getTypeEncoding(altMethod)); if (didAddMethod) { class_replaceMethod(class,altSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { method_exchangeImplementations(origMethod, altMethod); }return YES; }+ (BOOL)swizzleClassMethod:(Class)class orgSel:(SEL)origSel swizzSel:(SEL)altSel { Method origMethod = class_getClassMethod(class, origSel); Method altMethod = class_getClassMethod(class, altSel); if (!origMethod || !altMethod) { return NO; } BOOL didAddMethod = class_addMethod(object_getClass(class),origSel, method_getImplementation(altMethod), method_getTypeEncoding(altMethod)); if (didAddMethod) { class_replaceMethod(class,altSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { method_exchangeImplementations(origMethod, altMethod); }return YES; }

Demo:https://github.com/sy5075391/RuntimeSwizze.git

    推荐阅读