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 :为什么使用objc_getClass, 因为NSArray是类簇, 关于objectAtIndex,objectAtIndexedSubscript 方法的实现其实是_NSArrayI实现的,所以使用objc_getClass("__NSArrayI") 获取实际的类。不是类簇的类,是可以直接传入 self 或者[self class] 进行交换对象方法的。 ☆☆而此处要探讨的是为什么交换类方法可以使用
当obj为实例变量时,object_getClass(obj)与[obj class]输出结果一直,均获得isa指针,即指向类对象的指针。
当obj为类对象时,object_getClass(obj)返回类对象中的isa指针,即指向元类对象的指针;[obj class]返回的则是其本身
objc_getClass:
返回字符串对应的对象。和 NSClassFromString一致
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];
输出:
文章图片
输出 结果可以看出交换成功,继续深入
深入一下runtime(http://www.jianshu.com/p/54c190542aa8)
再自己瞎改瞎测试一下;
1.首先
NSObject的分类方法还是如上一样,如果不使用object_getClass("xxx")方式,那么交换类方法的代码改成
[self swizzleMethod:self orgSel:@selector(classMethod) swizzSel:@selector(swizzeClassMehtod)];
<----改动
经测试,果不其然交换类方法是交换失败的,
class_getInstanceMethod
取出的为origMethod
和altMethod
为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;
}
照这样写,
origMethod
和altMethod
不为空,但是一测试还是没有交换成功类方法。文章图片
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都是取对象方法因为通过object_getClass获取到的是类的元类,因为类也是对象,类方法对于元类来说就是是它的对象方法,当class为元类时,使用class_getInstanceMethod是可以取出方法的,取出的为类方法。class_getInstanceMethod
取的,但是如果取类方法应该是用class_getClassMethod
这个api。而上述分类为什么能交换到类方法?
2. 为什么类方法交换时要这样使用针对于文中最开始的的交换方法实现,对于交换实例方法,直接传入对象的class即可,而对于交换类方法,则需传入类的元类,object_getClass()取出的就是元类,才能进行交换。object_getClass("xxx")
调用 swizzleMethod
总结 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
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 你应该看透人生处处的交换
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密
- iOS,打Framework静态库