class_replaceMethod & method_exchangeImplementations

A,B 继承 Base

@interface Base : NSObject
- (void)print:(NSString*)msg;
- (void)hookPrint:(NSString*)msg;
@end
@implementation Base
- (void)print:(NSString*)msg{
    NSLog(@"print-->obj %@ print say:%@", NSStringFromClass(self.class), msg);
}
- (void)hookPrint:(NSString*)msg {
    NSLog(@"hookPrin-->obj %@ print say:%@", NSStringFromClass(self.class), msg);
}
@end

@interface ATest : Base
- (void)print:(NSString*)msg;
@end
@implementation ATest
- (void)print:(NSString*)msg {
    NSLog(@"A obj print say:%@", msg);
}
@end

@interface BTest : Base
@end
@implementation BTest
@end

1.class_replaceMethod

1.1具体替换方法实现
- (void)oneSwizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector
{    Class cls = [self class];
    Method originalMethod = class_getInstanceMethod(cls, origSelector);
    Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
    
    //class_replaceMethod 返回的IMP是 没有替代覆盖之前的IMP
    IMP previousIMP = class_replaceMethod(cls,
                                          origSelector,
                                          method_getImplementation(swizzledMethod),
                                          method_getTypeEncoding(swizzledMethod));
    //newSelector 跟 previousIMP(origSelector以前的实现)进行组合,所谓replaceMethod,其实跟像是强制更新覆盖
    //newSelector 方法名,previousIMP是实现(IMP)地址(origSelector以前的实现)覆盖掉现有的newSelectorIMP现在的实现
    if (previousIMP) {//如果previousIMP(origSelector以前的实现)存在,则用以前的imp 去跟 newSelector 组合
        class_replaceMethod(cls,
                            newSelector,
                            previousIMP,
                            method_getTypeEncoding(originalMethod));
    }else{//如果previousIMP(origSelector以前的实现)不存在,则用 寻找父类的同名方法imp 跟 newSelector 组合
        class_replaceMethod(cls,
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    }
}
1.2程序调用
 [ATest oneSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
 [BTest oneSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
    //测试代码是这样的:
 ATest *a1111 = [ATest new];
 [a1111 print:@"hello1"];
 [a1111 hookPrint:@"hello1"];
 BTest *b1111 = [BTest new];
 [b1111 print:@"hello2"];
 [b1111 hookPrint:@"hello2"];
 Base *base1 = [Base new];
 [base1 print:@"hello3"];
 [base1 hookPrint:@"hello3"];
1.3输出结果
2021-11-27 16:03:54.935814+0800 AddMethodDemo[16240:2459732] hookPrin-->obj ATest print say:hello1
2021-11-27 16:03:54.935986+0800 AddMethodDemo[16240:2459732] A obj print say:hello1
2021-11-27 16:03:54.936117+0800 AddMethodDemo[16240:2459732] hookPrin-->obj BTest print say:hello2
2021-11-27 16:03:54.936295+0800 AddMethodDemo[16240:2459732] print-->obj BTest print say:hello2
2021-11-27 16:03:54.936440+0800 AddMethodDemo[16240:2459732] print-->obj Base print say:hello3
2021-11-27 16:03:54.936561+0800 AddMethodDemo[16240:2459732] hookPrin-->obj Base print say:hello3

\color{red}{总结:}class_replaceMethod,方法交换,在当前类 有方法直接换,没有方法,会去父类中找,如果有方法名没有IMP就会报野指针,但不会影响父类。
用class_replaceMethod()实现的。由于A中不存在hookPrint方法,class_replaceMethod会调用class_addMethod方法,而class_addMethod会把Base的hookPrint实现添加到当前类,print的实现最终会和hookPrint的实现交换。B中俩方法都不存在,也会添加俩方法,最终交换俩方法的实现。最终打印的时候,会调用Base的hookPrint方法。


2. method_exchangeImplementations

2.1具体替换方法实现
- (void)twoSwizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
    Class cls = [self class];
    Method originalMethod = class_getInstanceMethod(cls, origSelector);
    Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
    method_exchangeImplementations(originalMethod, swizzledMethod);
}
2.2程序调用
[ATest twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
    [BTest twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
    //测试代码是这样的:
    ATest* a = [ATest new]; [a print:@"hello1"];
    BTest* b = [BTest new]; [b print:@"hello2"];
    Base *base2 = [Base new];
    [base2 print:@"hello3"];
2.3输出结果
2021-11-27 16:09:58.219639+0800 AddMethodDemo[16300:2463887] hookPrin-->obj ATest print say:hello1
2021-11-27 16:09:58.219816+0800 AddMethodDemo[16300:2463887] A obj print say:hello2
2021-11-27 16:09:58.219943+0800 AddMethodDemo[16300:2463887] A obj print say:hello3

\color{red}{总结:} method_exchangeImplementations,子类有存在交换方法的,优先,子类没有去父类中找,直接交换IMP很暴力。
用method_exchangeImplementations实现。由于A没有实现hookPrint方法,在调用
[A twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)]的时候,将A的print实现与Base的hookPrint实现交换了。
接着调用 [B twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)]的时候,由于B类啥都没实现,它只能将Base的print实现与base的hookPrint交换了。最终,调用[b print:@"hello2"]的时候,调用的是代码中A类的print方法的实现

method_exchangeImplementations交换的是 method_t里面的 IMP
class_rw_t
method_t

地址在项目草稿中。。。

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

推荐阅读更多精彩内容