[19]Effective Objective-C 2.0【13】

第十三條:用“方法調(diào)配技術(shù)”調(diào)試“黑盒方法”

什么是Method Swizzling?

字面意思:方法調(diào)和,也就是方法交換,其中交換的是方法的實(shí)現(xiàn)。具體點(diǎn)的來(lái)說(shuō),我們用@selector(方法選擇器) 取出來(lái)的是一個(gè)方法的編號(hào)(指向方法的指針) ,用SEL類(lèi)型表示,它所指向的是一個(gè)IMP(方法實(shí)現(xiàn)的指針) ,而我們交換的就是這個(gè)IMP,從而達(dá)到方法實(shí)現(xiàn)交換的效果。

1.當(dāng)一個(gè)方法在工程中大量被調(diào)用時(shí),我們想要批量替換或修改,那就很麻煩,有人說(shuō)直接修改這個(gè)方法的實(shí)現(xiàn),這種方式是不推薦的,因?yàn)檫@會(huì)破壞原有方法的完整性,而且也不是所有方法都能被修改,比如閉源,這個(gè)時(shí)候我們用Method Swizzling就可以很好的處理了。

2.通過(guò)運(yùn)行時(shí)的一些操作可以用另外一份實(shí)現(xiàn)來(lái)替換掉原有的方法實(shí)現(xiàn),往往被應(yīng)用在向原有實(shí)現(xiàn)中添加新功能,比如擴(kuò)展UIViewController,在viewDidLoad里面增加打印信息等。

舉幾個(gè)栗子

  • 1。友盟統(tǒng)計(jì)很多人用過(guò)吧,每個(gè)控制器頁(yè)面出現(xiàn)和消失都要做標(biāo)記,很煩!所以我們用Method Swizzling對(duì)viewWillAppear&viewWillDisappear跟我們自定義方法交換,然后統(tǒng)一處理
@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:)];
    });
}

/**
  頁(yè)面出現(xiàn)的時(shí)候會(huì)進(jìn)入到這里實(shí)現(xiàn),即使在子類(lèi)重寫(xiě)了ViewWillAppear:方法,
  那么在調(diào)用[super ViewWillAppear:animated]的時(shí)候還是會(huì)進(jìn)入這里。
  
  @param animated 動(dòng)畫(huà)
*/
- (void)zm_ViewWillAppear:(BOOL)animated{
     //此處調(diào)用自己其實(shí)就是調(diào)用UIViewController的viewWillAppear的原生實(shí)現(xiàn)方法。
     [self zm_ViewWillAppear:animated]; 
     //統(tǒng)一添加統(tǒng)計(jì)代碼
     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。對(duì)一些系統(tǒng)原生方法做調(diào)換,防止開(kāi)發(fā)過(guò)程中因不謹(jǐn)慎導(dǎo)致的crash,比如數(shù)組越界、數(shù)組和字典插入nil對(duì)象、字符串截取越界...,我們都可以在自定義方法中先做判斷,從而過(guò)濾掉這些不注意的bug,一勞永逸。這里只對(duì)一些常用的方法做處理

不可變數(shù)組:

@implementation NSArray (ZMSafe)

//數(shù)組初始化類(lèi)
static NSString *KInitArrayClass   = @"__NSPlaceholderArray";
//空元素?cái)?shù)組類(lèi),空數(shù)組
static NSString *KEmptyArrayClass  = @"__NSArray0";
//單元素?cái)?shù)組類(lèi),一個(gè)元素的數(shù)組
static NSString *KSingleArrayClass = @"__NSSingleObjectArrayI";
//多元素?cái)?shù)組類(lèi),兩個(gè)元素以上的數(shù)組
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的子類(lèi)
    // zm_multiArrayObjectAtIndex:是NSArray的方法,self是__NSArrayI的實(shí)例,子類(lèi)調(diào)用父類(lèi)的方法,沒(méi)問(wèn)題
    return [self zm_multiArrayObjectAtIndex:index];
}

//解決array[index] 字面量語(yǔ)法超出界限的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];
}

可變數(shù)組:

這里使用MRC寫(xiě)法,是為了修復(fù)[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];
    }
}

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

開(kāi)發(fā)中可以靈活運(yùn)用,但是也不能亂用,不然會(huì)出現(xiàn)意想不到的后果??

注意幾點(diǎn):

  • 最好在+(load)方法中來(lái)調(diào)用,因?yàn)?(load)能確保一定被調(diào)用,而且調(diào)用時(shí)機(jī)非常早,在裝載類(lèi)文件時(shí)就會(huì)被調(diào)用(程序啟動(dòng)前),所以在運(yùn)行時(shí)你的Method Swizzling一定是執(zhí)行了的。

  • 在執(zhí)行Method Swizzling的時(shí)候最好加上dispatch_once,雖然說(shuō)+(load)只會(huì)被系統(tǒng)調(diào)用一次,但是如果在子類(lèi)或子類(lèi)的子類(lèi)調(diào)用了[super load],那么父類(lèi)會(huì)再次調(diào)用,導(dǎo)致Method Swizzling多次執(zhí)行,換來(lái)?yè)Q去,到時(shí)候有沒(méi)有調(diào)換過(guò)來(lái)就看運(yùn)氣了,那就尷尬了??

  • 如果交換的是系統(tǒng)原生方法,一定要在自定義實(shí)現(xiàn)方法中調(diào)用自身方法,也就是原生實(shí)現(xiàn)方法,因?yàn)槲覀儾皇且麄€(gè)重寫(xiě)原生實(shí)現(xiàn)方法,而是在它基礎(chǔ)之上添加我們自己的東西;如果交換的是自定義方法,那就看需求而定咯??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容