[19]Effective Objective-C 2.0【13】

第十三条:用“方法调配技术”调试“黑盒方法”

什么是Method Swizzling?

字面意思:方法调和,也就是方法交换,其中交换的是方法的实现。具体点的来说,我们用@selector(方法选择器) 取出来的是一个方法的编号(指向方法的指针) ,用SEL类型表示,它所指向的是一个IMP(方法实现的指针) ,而我们交换的就是这个IMP,从而达到方法实现交换的效果。

1.当一个方法在工程中大量被调用时,我们想要批量替换或修改,那就很麻烦,有人说直接修改这个方法的实现,这种方式是不推荐的,因为这会破坏原有方法的完整性,而且也不是所有方法都能被修改,比如闭源,这个时候我们用Method Swizzling就可以很好的处理了。

2.通过运行时的一些操作可以用另外一份实现来替换掉原有的方法实现,往往被应用在向原有实现中添加新功能,比如扩展UIViewController,在viewDidLoad里面增加打印信息等。

举几个栗子

  • 1。友盟统计很多人用过吧,每个控制器页面出现和消失都要做标记,很烦!所以我们用Method Swizzling对viewWillAppear&viewWillDisappear跟我们自定义方法交换,然后统一处理
@implementation UIViewController (Swizzle)
  
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       [self zm_swizzleInstanceMethodWithSrcClass:[self class]
                                           srcSel:@selector(viewWillAppear:)
                                      swizzledSel:@selector(zm_ViewWillAppear:)];
       [self zm_swizzleInstanceMethodWithSrcClass:[self class]
                                           srcSel:@selector(viewWillDisappear:)
                                      swizzledSel:@selector(zm_ViewWillDisappear:)];
    });
}

/**
  页面出现的时候会进入到这里实现,即使在子类重写了ViewWillAppear:方法,
  那么在调用[super ViewWillAppear:animated]的时候还是会进入这里。
  
  @param animated 动画
*/
- (void)zm_ViewWillAppear:(BOOL)animated{
     //此处调用自己其实就是调用UIViewController的viewWillAppear的原生实现方法。
     [self zm_ViewWillAppear:animated]; 
     //统一添加统计代码
     self.title.length == 0?:[MobClick beginLogPageView:self.title];
}

- (void)zm_ViewWillDisappear:(BOOL)animated{
     [self zm_ViewWillDisappear:animated];
     self.title.length == 0?:[MobClick endLogPageView:self.title];
}

  • 2。对一些系统原生方法做调换,防止开发过程中因不谨慎导致的crash,比如数组越界、数组和字典插入nil对象、字符串截取越界...,我们都可以在自定义方法中先做判断,从而过滤掉这些不注意的bug,一劳永逸。这里只对一些常用的方法做处理

不可变数组:

@implementation NSArray (ZMSafe)

//数组初始化类
static NSString *KInitArrayClass   = @"__NSPlaceholderArray";
//空元素数组类,空数组
static NSString *KEmptyArrayClass  = @"__NSArray0";
//单元素数组类,一个元素的数组
static NSString *KSingleArrayClass = @"__NSSingleObjectArrayI";
//多元素数组类,两个元素以上的数组
static NSString *KMultiArrayClass  = @"__NSArrayI";

#define KSelectorFromString(s1,s2) NSSelectorFromString([NSString stringWithFormat:@"%@%@",s1,s2])

+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
  
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KInitArrayClass)
                                      srcSel:@selector(initWithObjects:count:)
                                 swizzledSel:@selector(zm_safeInitWithObjects:count:)];
  
        [self zm_arrayMethodSwizzleWithRealClass:KEmptyArrayClass prefix:@"zm_emptyArray"];
        [self zm_arrayMethodSwizzleWithRealClass:KSingleArrayClass prefix:@"zm_singleArray"];
        [self zm_arrayMethodSwizzleWithRealClass:KMultiArrayClass prefix:@"zm_multiArray"];
  
    });

}

+ (void)zm_arrayMethodSwizzleWithRealClass:(NSString *)realClass prefix:(NSString *)prefix
{

    [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(realClass)
                                  srcSel:@selector(objectAtIndex:)
                             swizzledSel:KSelectorFromString(prefix, @"ObjectAtIndex:")];

    [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(realClass)
                                  srcSel:@selector(arrayByAddingObject:)
                             swizzledSel:KSelectorFromString(prefix, @"ArrayByAddingObject:")];

    if (iOS11) {
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(realClass)
                                      srcSel:@selector(objectAtIndexedSubscript:)
                                 swizzledSel:KSelectorFromString(prefix, @"ObjectAtIndexedSubscript:")];
    }
}

#pragma mark -- swizzled Methods
- (instancetype)zm_safeInitWithObjects:(id *)objects count:(NSUInteger)cnt
{
    for (NSUInteger i = 0; i < cnt; i++)
    {
        if (!objects[i]) objects[i] = @"";
    }
    return [self zm_safeInitWithObjects:objects count:cnt];
}

- (id)zm_emptyArrayObjectAtIndex:(NSUInteger)index
{
    if (index >= self.count) return nil;
    return [self zm_emptyArrayObjectAtIndex:index];
}

- (id)zm_singleArrayObjectAtIndex:(NSUInteger)index
{
    if (index >= self.count) return nil;
    return [self zm_singleArrayObjectAtIndex:index];
}

- (id)zm_multiArrayObjectAtIndex:(NSUInteger)index
{
    if (index >= self.count) return nil;
     // NSLog(@"%@",NSStringFromClass(self.class));      //__NSArrayI
    // NSLog(@"%@",NSStringFromClass(self.superclass)); //NSArray
    // __NSArrayI是NSArray的子类
    // zm_multiArrayObjectAtIndex:是NSArray的方法,self是__NSArrayI的实例,子类调用父类的方法,没问题
    return [self zm_multiArrayObjectAtIndex:index];
}

//解决array[index] 字面量语法超出界限的bug
- (id)zm_emptyArrayObjectAtIndexedSubscript:(NSUInteger)index
{
    if (index >= self.count) return nil;
    return [self zm_emptyArrayObjectAtIndexedSubscript:index];
}

- (id)zm_singleArrayObjectAtIndexedSubscript:(NSUInteger)index
{
     if (index >= self.count) return nil;
    return [self zm_singleArrayObjectAtIndexedSubscript:index];
}

- (id)zm_multiArrayObjectAtIndexedSubscript:(NSUInteger)index
{
    if (index >= self.count) return nil;
    return [self zm_multiArrayObjectAtIndexedSubscript:index];
}

- (NSArray*)zm_emptyArrayArrayByAddingObject:(id)anObject
{
    if(!anObject) return self;
    return [self zm_emptyArrayArrayByAddingObject:anObject];
}

- (NSArray*)zm_singleArrayArrayByAddingObject:(id)anObject
{
    if(!anObject) return self;
    return [self zm_singleArrayArrayByAddingObject:anObject];
}

- (NSArray*)zm_multiArrayArrayByAddingObject:(id)anObject
{
    if(!anObject) return self;
    return [self zm_multiArrayArrayByAddingObject:anObject];
}

可变数组:

这里使用MRC写法,是为了修复[UIKeyboardLayoutStar release]: message sent to deallocated instance的Bug,所以该文件需要添加ARC支持-fno-objc-arc

@implementation NSMutableArray (ZMSafe)
static NSString *KMArrayClass = @"__NSArrayM";

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    @autoreleasepool {
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass)
                                          srcSel:@selector(addObject:)
                                     swizzledSel:@selector(zm_safeAddObject:)];
      
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass)
                                          srcSel:@selector(insertObject:atIndex:)
                                     swizzledSel:@selector(zm_safeInsertObject:atIndex:)];
      
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass)
                                          srcSel:@selector(removeObjectAtIndex:)
                                     swizzledSel:@selector(zm_safeRemoveObjectAtIndex:)];
      
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass)
                                          srcSel:@selector(replaceObjectAtIndex:withObject:)
                                     swizzledSel:@selector(zm_safeReplaceObjectAtIndex:withObject:)];
      
        [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass)
                                          srcSel:@selector(objectAtIndex:)
                                     swizzledSel:@selector(zm_safeObjectAtIndex:)];

        if (iOS11) {
          [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass)
                                              srcSel:@selector(objectAtIndexedSubscript:)
                                         swizzledSel:@selector(zm_safeObjectAtIndexedSubscript:)];
        }
     
    });
}

- (void)zm_safeAddObject:(id)anObject{
     @autoreleasepool {
       if(!anObject)return;
  
      [self zm_safeAddObject:anObject];
     }

}

- (void)zm_safeInsertObject:(id)anObject atIndex:(NSUInteger)index{
     @autoreleasepool {
        if(!anObject || index > self.count)return;
  
        [self zm_safeInsertObject:anObject atIndex:index];
     }

}

- (void)zm_safeRemoveObjectAtIndex:(NSUInteger)index{
     @autoreleasepool {
        if(index >= self.count) return;
  
        [self zm_safeRemoveObjectAtIndex:index];
     }

}

- (void)zm_safeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject{
     @autoreleasepool {
        if(index >= self.count || !anObject) return;
  
        [self zm_safeReplaceObjectAtIndex:index withObject:anObject];
     }

}

- (id)zm_safeObjectAtIndex:(NSUInteger)index{
     @autoreleasepool {
        if (index >= self.count) return nil;
  
        return [self zm_safeObjectAtIndex:index];
     }

}

- (id)zm_safeObjectAtIndexedSubscript:(NSUInteger)index{
     @autoreleasepool {
        if (index >= self.count) return nil;
  
        return [self zm_safeObjectAtIndexedSubscript:index];
    }
}

不可变字典,可变字典,不可变字符串,可变字符串等。。

开发中可以灵活运用,但是也不能乱用,不然会出现意想不到的后果

注意几点:

  • 最好在+(load)方法中来调用,因为+(load)能确保一定被调用,而且调用时机非常早,在装载类文件时就会被调用(程序启动前),所以在运行时你的Method Swizzling一定是执行了的。

  • 在执行Method Swizzling的时候最好加上dispatch_once,虽然说+(load)只会被系统调用一次,但是如果在子类或子类的子类调用了[super load],那么父类会再次调用,导致Method Swizzling多次执行,换来换去,到时候有没有调换过来就看运气了,那就尴尬了

  • 如果交换的是系统原生方法,一定要在自定义实现方法中调用自身方法,也就是原生实现方法,因为我们不是要整个重写原生实现方法,而是在它基础之上添加我们自己的东西;如果交换的是自定义方法,那就看需求而定咯

你可能感兴趣的:([19]Effective Objective-C 2.0【13】)