NSInvalidArgumentException導(dǎo)致的APP閃退

我們?cè)诔跏蓟疦SDictionary時(shí),常常會(huì)使用如下方式:

NSDictionary *dict = @{
                        @"key":value ,
                        @"key1":value1
                       };

因?yàn)檫@種方式方便簡(jiǎn)潔,而且鍵值對(duì)的關(guān)系一目了然.但是在很多情況下我們傳進(jìn)來的value是動(dòng)態(tài)的,并不能保證是否為nil,結(jié)果就會(huì)導(dǎo)致如下crash:

**Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]
: attempt to insert nil object from objects[0]'**

解決這種情況的方案有三種:

  • 方案一

在每次加入value之前判斷是否為nil,這也是我們之前比較常用的解決辦法.

  • 方案二

不使用@{ }這種形式來初始化NSDictionary,而是使用更為安全的方式

[NSDictionary dictionaryWithObjectsAndKeys:value,@"key", nil];
  • 方案三

使用runtime的機(jī)制.
給NSDictionary寫個(gè)分類完美解決,原項(xiàng)目的代碼什么都不用改.分類下載鏈接https://pan.baidu.com/s/1hsXmIv6

#import <Foundation/Foundation.h>
#import <objc/message.h>

@implementation NSDictionary (ZCReplaceNullValue)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [objc_getClass("__NSPlaceholderDictionary") swizzleSelector:@selector(initWithObjects:forKeys:count:) withSwizzledSelector:@selector(safeInitWithObjects:forKeys:count:)];
    });
}

- (instancetype)safeInitWithObjects:(const id _Nonnull __unsafe_unretained *)objects forKeys:(const id _Nonnull __unsafe_unretained *)keys count:(NSUInteger)cnt {
    BOOL containNilObject = NO;
    for (NSUInteger i = 0; i < cnt; i++) {
        if (objects[i] == nil) {
            containNilObject = YES;
            NSLog(@"reason: ***object cannot be nil (key: %@)", keys[i]);
        }
    }
    if (containNilObject) {
        NSUInteger nilCount = 0;
        for (NSUInteger i = 0; i < cnt; ++i) {
            if (objects[i] == nil) {
                nilCount ++;
            }
        }
        NSUInteger length = cnt - nilCount;
        if (length > 0) {
            NSUInteger index = 0;
            id __unsafe_unretained newObjects[length];
            id __unsafe_unretained newKeys[length];
            for (NSUInteger i = 0; i < cnt; ++i) {
                if (objects[i] != nil) {
                    newObjects[index] = objects[i];
                    newKeys[index] = keys[i];
                    index ++ ;
                }
            }
            NSLog(@"fixedDictionary:%@",[self safeInitWithObjects:newObjects forKeys:newKeys count:length]);
            return [self safeInitWithObjects:newObjects forKeys:newKeys count:length];
        } else {
            NSLog(@"fixedDictionary:nil (all objects are nil)");
            return nil;
        }
    }
    return [self safeInitWithObjects:objects forKeys:keys count:cnt];
}

+ (void)swizzleSelector:(SEL)originalSelector withSwizzledSelector:(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);
    }
}

@end

注:這種方式也只能解決使用@{}形式初始化NSDictionary導(dǎo)致的NSInvalidArgumentException崩潰

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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