正确的Swizzle方法

通常运行时用一个方法替换另外一个方法的行为,称为Swizzling。
有很多的原因我们需要使用到Swizzling,自我检测,重载某个行为,动态加载。


之前看到过很多博客有提到如何实现Swizzling,大部分方式不够好。
假如单写一个独立的应用,上述的Swizzling实现还是在自己的掌控中。
但是写一个framework提供给第3方使用,简单的Swizzling实现往往会把问题搞乱。


从基础开始,当看到Swizzling时,意味着用自己的方法替换原来的方法。
Object-C Runtime允许这种实践。Object-C方法用一个struct objc_method来表示,
typedef成Method。


struct objc_method
     SEL method_name         OBJC2_UNAVAILABLE;
     char *method_types      OBJC2_UNAVAILABLE;
     IMP method_imp          OBJC2_UNAVAILABLE;
}


method_name   表示方法的selector,
*method_types 是一个c字符串,包括参数和返回值的类型
method_imp    是一个函数指针,指向真正的实现。


用以下的方法可以访问Method


Method class_getClassMethod(Class aClass, SEL aSelector);
Method class_getInstanceMethod(Class aClass, SEL aSelector);


method_imp 是一个定义为 id (*IMP)(id, SEL, …)的IMP类型函数指针。
这个IMP数值可以用IMP method_setImplementation(Method method, IMP imp)函数替换。
通过method_setImplementation()改变method结构中的method_imp, 而保留method结构中的其他数值,
这才是swizzle的正确方法。




通常有一个强大的方法来做swizzle,看似直接简单,其实有不可测的未知性。


void method_exchangeImplementations(Method m1, Method m2);


要理解这种不可预测性,我们看在调用前的两个m1, m2 Method结构。


Method m1 { //this is the original method. we want to switch this one with
             //our replacement method
      SEL method_name = @selector(originalMethodName)
      char *method_types = “v@:“ //returns void, params id(self),selector(_cmd)
      IMP method_imp = 0x000FFFF (MyBundle`[MyClass originalMethodName])
 }


Method m2 { //this is the swizzle method. We want this method executed when [MyClass
             //originalMethodName] is called
       SEL method_name = @selector(swizzle_originalMethodName)
       char *method_types = “v@:”
       IMP method_imp = 0x1234AABA (MyBundle`[MyClass swizzle_originalMethodName])
 }


Objective-C 代码看起来大致如下:


@implementation MyClass
     - (void) originalMethodName //m1
     {
              //code
     }
     - (void) swizzle_originalMethodName //m2
     {
             //…code?
            [self swizzle_originalMethodName];//call original method
            //…code?
     }
 @end


运行,


m1 = class_getInstanceMethod([MyClass class], @selector(originalMethodName));
m2 = class_getInstanceMethod([MyClass class], @selector(swizzle_originalMethodName));
method_exchangeImplementations(m1, m2);


现在m1, m2结构如下,


Method m1 { //this is the original Method struct. we want to switch this one with
             //our replacement method
     SEL method_name = @selector(originalMethodName)
     char *method_types = “v@:“ //returns void, params id(self),selector(_cmd)
     IMP method_imp = 0x1234AABA (MyBundle`[MyClass swizzle_originalMethodName])
 }


Method m2 { //this is the swizzle Method struct. We want this method executed when [MyClass
            //originalMethodName] is called
     SEL method_name = @selector(swizzle_originalMethodName)
     char *method_types = “v@:”
     IMP method_imp = 0x000FFFF (MyBundle`[MyClass originalMethodName])
 }


注意现在假如我们要执行原始方法,需要调用 


[self swizzle_originalMethodName];


结果是,传入原始方法的_cmd变成了 @selector(swizzle_originalMethodName), 假如方法代码里依赖_cmd的话,
这种swizzle结果导致了不可预知性,这是我们需要避免的。


- (void) originalMethodName //m1
 {
          assert([NSStringFromSelector(_cmd) isEqualToString:@“originalMethodNamed”]); //this fails after swizzling //using
          //method_exchangedImplementations()
          //…
 }
查看 NSStringFromSelector(_cmd), 这里变成了 @“swizzle_originalMethodName”.


现在我们再看看正确的swizzling,使用setImplementation()函数。


The correct way to swizzling,


创建一个符合IMP定义的c函数来代替Object-C的类方法,-(void)swizzle_orignalMethodName;


void __Swizzle_OriginalMethodName(id self, SEL _cmd)
 {
      //code
 }


我们可以把该函数附值给一个IMP。


IMP swizzleImp = (IMP)__Swizzle_OriginalMethodName;


再把swizzleImp传给 method_setImplementation(),


method_setImplementation(method, swizzleImp);


method_setImplementation()返回的是原始IMP(original),


IMP originalImp = method_setImplementation(method,swizzleImp);


现在,我们再调用原始方法试试。


originalImp(self,_cmd);




完整代码例子,


@interface SwizzleExampleClass : NSObject
 - (void) swizzleExample;
 - (int) originalMethod;
@end


static IMP __original_Method_Imp;
int _replacement_Method(id self, SEL _cmd)
{
    assert([NSStringFromSelector(_cmd) isEqualToString:@"originalMethod"]);
    
    //code
    int returnValue = ((int(*)(id,SEL))__original_Method_Imp)(self, _cmd);
    return returnValue + 1;
}


@implementation SwizzleExampleClass
- (void) swizzleExample //call me to swizzle
{
     Method m = class_getInstanceMethod([self class], @selector(originalMethod));
     __original_Method_Imp = method_setImplementation(m, (IMP)_replacement_Method);
}


- (int) originalMethod
{
     //code
     assert([NSStringFromSelector(_cmd) isEqualToString:@"originalMethod"]);
     return 1;
}
@end


测试代码:


SwizzleExampleClass* example = [[SwizzleExampleClass alloc] init];
int originalReturn = [example originalMethod];
[example swizzleExample];
int swizzledReturn = [example originalMethod];
assert(originalReturn == 1); //true
assert(swizzledReturn == 2); //true


记得用以下方法验证:

    Method m = class_getInstanceMethod([exampleclass], @selector(originalMethod));

    structobjc_method_description *desc = method_getDescription(m);


    NSLog(@"Before and after  swizzle");

    NSLog(@"Method selector name, %@",NSStringFromSelector(desc->name));

    NSLog(@"Method selector types, %s", desc->types);




结论是为避免第3方库,不要swizzle Object-C方法,还有method_exchangeImplementations(), 代替使用c函数和method_setImplementation(), 
把c函数附值给IMP变量。这样可以避免掉来自Object-C方法或新的selector名称产生的命名包袱。


不要忘了,所有Object-C方法有两个隐藏的参数,self和_cmd。


你还需要注意返回是void的IMP调用。ARC假设所有IMP返回一个id对象。


IMP anImp; //represents objective-c function


// -UIViewController viewDidLoad;
((void(*)(id,SEL))anImp)(self,_cmd); //call with a cast to prevent

                                     // ARC from retaining void.


原文链接:

http://blog.newrelic.com/2014/04/16/right-way-to-swizzle/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值