美文网首页
NSInvalidArgumentException导致的APP

NSInvalidArgumentException导致的APP

作者: _Miyo | 来源:发表于2018-01-10 11:51 被阅读0次

    我们在初始化NSDictionary时,常常会使用如下方式:

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

    因为这种方式方便简洁,而且键值对的关系一目了然.但是在很多情况下我们传进来的value是动态的,并不能保证是否为nil,结果就会导致如下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的机制.
    给NSDictionary写个分类完美解决,原项目的代码什么都不用改.分类下载链接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导致的NSInvalidArgumentException崩溃

    相关文章

      网友评论

          本文标题:NSInvalidArgumentException导致的APP

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