iOS runtime系列三 -- Method Swizzling

Method Swizzling参考资料

1.用到的运行时基础知识介绍

  • SEL : 方法选择器,SEL是函数objc_msgSend第二个参数的数据类型,表示方法选择器 ;其实它就是映射到方法的C字符串,你可以通过Objc编译器命令@selector()/NSSelectorFromString()或者Runtime系统的sel_registerName函数来获取一个SEL类型的方法选择器
  • Method : 就是一个指向objc_method结构体指针,它存储了方法名(method_name)、方法类型(method_types)和一个指向方法实现的函数指针(method_imp)等信息
  • IMP : IMP本质上就是一个函数指针,指向方法的实现,当你向某个对象发送一条信息,可以由这个函数指针来指定方法的实现,它最终就会执行那段代码,这样可以绕开消息传递阶段而去执行另一个方法实现
  • class_getInstanceMethod(Class cls, SEL name),获取指定类中,方法选择器对应的方法,需要注意在搜索方法的时候,会先从指定类的方法列表中搜索,如果搜索不到(当前类没有实现此方法),就会去superclasses 中搜索
/** 
 * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
 */
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
  • OBJC_EXPORT IMP method_getImplementation(Method m)
    获取某个方法对应的方法实现
  • OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) 为该类添加一个方法,但不会去重写已有实现的方法,如果已经有相同方法名称则会返回NO;注意:父类中有对应的方法而当前类没有,会添加成功
/** 
 * Adds a new method to a class with a given name and implementation.
 * 
 * @param cls The class to which to add a method.
 * @param name A selector that specifies the name of the method being added.
 * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
 * @param types An array of characters that describe the types of the arguments to the method. 
 * 
 * @return YES if the method was added successfully, otherwise NO 
 *  (for example, the class already contains a method implementation with that name).
 *
 * @note class_addMethod will add an override of a superclass's implementation, 
 *  but will not replace an existing implementation in this class. 
 *  To change an existing implementation, use method_setImplementation.
 */
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, 
                                 const char *types) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
  • OBJC_EXPORT IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types) ,替换某个类中方法选择器对应方法的方法实现
/** 
 * Replaces the implementation of a method for a given class.
 * 
 * @param cls The class you want to modify.
 * @param name A selector that identifies the method whose implementation you want to replace.
 * @param imp The new implementation for the method identified by name for the class identified by cls.
 * @param types An array of characters that describe the types of the arguments to the method. 
 *  Since the function must take at least two arguments—self and _cmd, the second and third characters
 *  must be “@:” (the first character is the return type).
 * 
 * @return The previous implementation of the method identified by \e name for the class identified by \e cls.
 * 
 * @note This function behaves in two different ways:
 *  - If the method identified by \e name does not yet exist, it is added as if \c class_addMethod were called. 
 *    The type encoding specified by \e types is used as given.
 *  - If the method identified by \e name does exist, its \c IMP is replaced as if \c method_setImplementation were called.
 *    The type encoding specified by \e types is ignored.
 */
OBJC_EXPORT IMP class_replaceMethod(Class cls, SEL name, IMP imp, 
                                    const char *types) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
  • method_exchangeImplementations(Method m1, Method m2) 交换两个方法的方法实现
/** 
 * Exchanges the implementations of two methods.
 * 
 * @param m1 Method to exchange with second method.
 * @param m2 Method to exchange with first method.
 * 
 * @note This is an atomic version of the following:
 *  \code 
 *  IMP imp1 = method_getImplementation(m1);
 *  IMP imp2 = method_getImplementation(m2);
 *  method_setImplementation(m1, imp2);
 *  method_setImplementation(m2, imp1);
 *  \endcode
 */
OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);

2. 方法交换

2.1 写法一:有缺陷版本,不建议使用

+ (void)load
{
        Class class = [self class];
        SEL originalSelector = @selector(testButtonLog);
        SEL swizzledSelector = @selector(xl_testButtonLog);
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        method_exchangeImplementations(originalMethod, swizzledMethod);    
}

2.2 写法二: 建议写法,交换前用class_addMethod进行一层判断处理

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(testButtonLog);
        SEL swizzledSelector = @selector(xl_testButtonLog);
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod = class_addMethod(class,originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

2.3 比较两种写法差异

  • dispatch_once:保证代码块只会被执行一次并且线程安全,不过load方法中并不需要,并不会被多次调用
  • 为什么需要用到class_addMethod
  • 我们想要对一个类的某个方法进行扩展,应该把这种扩展限制在当前类和当前类的子类中,不应该影响到父类中的方法,因为这个父类可能还有其他派生类,修改了父类的方法可能造成一连串非预期的结果
  • 异常场景 : 假设我们要扩展的方法(testButtonLog)是继承自父类(TestButton)的,且当前类没有对此方法(testButtonLog)进行重写,按照写法一实现方法交换,最终实现的结果是把父类(TestButton)中的方法(testButtonLog)和方法(xl_testButtonLog)进行了交换,这样造成的后果如下:
  • 如果是当前类(或当前类的子类)的实例对象调用 testButtonLog时,运行时会去执行 xl_testButtonLog , 没有什么问题
  • 如果是当前类的父类的实例对象调用testButtonLog时,会有什么后果? 会调用当前类的xl_testButtonLog方法,如果我们在xl_testButtonLog方法中这样写
```
- (void)xl_testButtonLog

{
NSLog(@"%s",func);
[self xl_testButtonLog]; // 调用此方法不会造成递归调用,因为上面做了方法交换,调用此方法,在运行时会去执行testButtonLog
}
```
会闪退,报错xl_testButtonLog方法找不到,原因:当前类的父类中并没有xl_testButtonLog方法具体可以看MethodSwizzlingDemo中情景2.2示例

闪退代码示例

  • 使用class_addMethod后,如果当前类没有实现要扩展的方法(testButtonLog), class_addMethod会自动在当前类中添加一个方法(testButtonLog),这样交换后的结果就是当前类中的方法(testButtonLog)和方法(xl_testButtonLog)进行交换,不影响父类testButtonLog的正常调用
  • 具体效果演示,可以参考demoMethodSwizzlingDemo中三种情景的演示

3. 扩展工具类

  • 对于Method Swizzling的实现方法可以抽取出来,封装到NSObject的分类中,方便以后使用,具体看demoMethodSwizzlingDemo中的NSObject+MethodSwizzling类
  • 交换两个实例方法
/**
交换两个实例方法
*/
+ (void)xl_exchangeInstanceMethod1:(SEL)originalSelector method2:(SEL)swizzledSelector
{
   Class class = [self class]; // 这个地方要注意
   Method originalMethod = class_getInstanceMethod(class, originalSelector);
   Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
   
   BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
   
   if (didAddMethod) {
       class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
   } else {
       method_exchangeImplementations(originalMethod, swizzledMethod);
   }
}
  • 使用示例:

    image.png

  • 交换两个类方法

/**
交换两个类方法
*/
+ (void)xl_exchangeClassMethod1:(SEL)originalSelector method2:(SEL)swizzledSelector
{
   Class class = object_getClass((id)self); // 这个地方要注意  
   Method originalMethod = class_getClassMethod(class, originalSelector);
   Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
   
   BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
   if (didAddMethod) {
       class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
   } else {
       method_exchangeImplementations(originalMethod, swizzledMethod);
   }
}
  • 注意点:交换类方法时,获取类要用object_getClass((id)self)方法获取class, 注意和通过[self class]获取的class是不同的对象,可以通过打印hash值来看
Class class = object_getClass((id)self); // 这个地方要注意
Class class2 = [self class];

 NSLog(@" class = %@ , class hash = %zd",class,[class hash]);
 NSLog(@" class2 = %@ , class2 hash = %zd",class2,[class2 hash]);
    //2017-05-17 18:38:00.171 EHGhostDrone3[26572:677703]  class = NSObject , class hash =  4663643704
    //2017-05-17 18:38:00.171 EHGhostDrone3[26572:677703]  class2 = NSObject , class2 hash = 4663643784

4. Method Swizzling的弊端

Method Swizzling就像一把瑞士小刀,如果使用得当,它会有效地解决问题。但使用不当,将带来很多麻烦。在stackoverflow上有人已经提出这样一个问题:What are the Dangers of Method Swizzling in Objective C?,它的危险性主要体现以下几个方面:

Method swizzling is not atomic
Changes behavior of un-owned code
Possible naming conflicts
Swizzling changes the method's arguments
The order of swizzles matters
Difficult to understand (looks recursive)
Difficult to debug

5. Method Swizzling 项目实践应用

5.1 自动处理按钮高亮和不可点击状态的属性

5.2 自动采集控制器的进入,退出和销毁等动作

5.3 自动采集按钮的点击事件

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

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,633评论 0 9
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,109评论 0 7
  • Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的...
    有一种再见叫青春阅读 552评论 0 3
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 1,327评论 0 8
  • 原文出处:南峰子的技术博客 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了...
    _烩面_阅读 1,192评论 1 5