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的原因

为了便于测试,自己定义了一个类 以及分类,仿照NSArray 的交换写法

#import <Foundation/Foundation.h>
@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取出的为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 不为空,但是一测试还是没有交换成功类方法。

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279

推荐阅读更多精彩内容