美文网首页
Method Swizzling

Method Swizzling

作者: A维生素A | 来源:发表于2018-06-01 16:23 被阅读0次

    Method Swizzling是runtime应用的体现,那么关于Method Swizzling的应用和注意事项在下面做简单的总结:

    Method Swizzling原理:

    每个类都维护一个方法(Method)列表,Method则包含SEL和其对应IMP的信息,方法交换做的事情就是把SEL和IMP的对应关系断开,并和新的IMP生成对应关系。

    交换前:Asel->AImp Bsel->BImp

    交换后:Asel->BImp Bsel->AImp

    Method Swizzling用途:

    1、面向切面编程: 数据统计;比如为了统计viewwillappear调用的次数,我们可以在基类(其他VC继承的类)的VC里面,添加如下代码就可以统计viewwillappear被调用的次数:

    +(void)load{//load方法在main()函数执行前就被执行//确保里面的方法被执行一次

        static dispatch_once_t onceToken;    

    dispatch_once(&onceToken, ^{       

     [self swizzingClass:[self class] originSel:@selector(viewWillAppear:) newSel:@selector(custom_viewWillAppear:)];   

     });

    }

    +(void)swizzingClass:(Class)class  originSel:(SEL)originSel  newSel:(SEL)newSel{    Method originM = class_getInstanceMethod(class, originSel);    

    Method newM = class_getInstanceMethod(class, newSel);   

     IMP newImp = method_getImplementation(newM);   

     BOOL addMethodSuccess = class_addMethod(class, newSel, newImp, method_getTypeEncoding(newM));    

    if (addMethodSuccess) {       

     class_replaceMethod(class, originSel, newImp, method_getTypeEncoding(newM));    }else{        

    method_exchangeImplementations(originM, newM);   

     }  

     }

    -(void)custom_viewWillAppear:(BOOL)animate{  

      [super viewWillAppear:animate];    

    NSLog(@"%@========%s",[self class],__func__);

    }

    2、数组越界问题 。

    法一   通过分类强化 :

    @implementation UIView (safe)

    - (BOOL)containsObjectAtIndex:(NSInteger)index {

    return index >= 0 && index 

    }

    - (id)objectNilAtIndex:(NSInteger)index{

    return [self containsObjectAtIndex:index] ? [self objectAtIndex:index] : nil;

    }

    @end

    法二  使用Method sizzling

    @implementation NSArray (StrengThen)

    + (void)load{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

    @autoreleasepool {

    [objc_getClass("__NSArray0") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(emptyObjectIndex:)];

    [objc_getClass("__NSArrayI") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(arrObjectIndex:)];

    [objc_getClass("__NSArrayM") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(mutableObjectIndex:)];

    [objc_getClass("__NSArrayM") swizzleMethod:@selector(insertObject:atIndex:) swizzledSelector:@selector(mutableInsertObject:atIndex:)];

    }

    });

    }

    - (id)emptyObjectIndex:(NSInteger)index{

    return nil;}

    - (id)arrObjectIndex:(NSInteger)index{

    if (index >= self.count || index < 0) {

    return nil;

    return [self arrObjectIndex:index];

    }

    - (id)mutableObjectIndex:(NSInteger)index{

    if (index >= self.count || index < 0) {

    return nil;

    }

    return [self mutableObjectIndex:index];

    }

    - (void)mutableInsertObject:(id)object atIndex:(NSUInteger)index{

    if (object) {

    [self mutableInsertObject:object atIndex:index];

    }

     }

     3、给全局图片名称添加后缀,比如你的工程所有的图片都更新了,以前都叫xxx.png现在叫xxx_new.png那么如果我们在工程中一张一张改名字比较麻烦,所以这个时候可以用“黑魔法”来达到相应的效果。(注意这个方法使用过后三方SDK里面引用的图片可能也会被改变,所以要谨慎使用,综合考虑下SDK和自己的图片数量占比,如果真的想使用就可以在三方SDK中的Bundle图片资源中,修改三方图片的名字)。

    Method Swizzling注意事项

    1、对自己使用Method Swizzling的地方要及时告诉同伴,否则就会在他人调用到此块方法的时候就会不知所以然。

    2、尽量少用Method Swizzling。虽然Method Swizzling可以让我们高效地解决某些问题,但是如果应用不得当,可能会引发一系列问题。

    3、swizzling 需要在 + (void)load{}中使用:

         在+(void)load{}方法中实现,这样可以保证方法一定会调用且不会出现异常;使用dispatch_once来执行方法交换,这样可以保证只运行一次。load 和initialize区别:load是只要类所在文件被引用就会被执行,而initialize是在类或者其子类的第一个方法被调用前调用。所以只有当此类没有被引用进项目时,才不会调用+(void)load{}方法;如果类文件被引用进来,但是没有使用,那么initialize也不会被调用;而此时+(void)load{}方法会被调用(在main()函数之前)。

    相关文章

      网友评论

          本文标题:Method Swizzling

          本文链接:https://www.haomeiwen.com/subject/gddksftx.html