chenfanfang / AvoidCrash

This framework can effective avoid crash by potential error code. For example : If you insert a nil into a mutable array, this framework can avoid crash and note you that where cause crash.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

使用下面的方式还是会crash,没有Avoid掉

smalltask opened this issue · comments

使用下面的方式还是会crash,没有Avoid掉
我在application:didFinishLaunchingWithOptions:中的第一行写了:
[AvoidCrash becomeEffective];

  NSDictionary *testDict = @{@"dic0":[NSNull null],@"dic1":@[@"arr10",@"arr1",[NSNull null]]};
    [[NSUserDefaults standardUserDefaults] setValue:testDict forKey:@"testDict"];
    [[NSUserDefaults standardUserDefaults] synchronize];

[User Defaults] Attempt to set a non-property-list object {
dic0 = "";
dic1 = (
arr10,
arr1,
""
);
} as an NSUserDefaults/CFPreferences value for key testDict

有空我写个解决方案给你,可以自己添加没有拦截的crash

非常感谢,我现在是写了两个category,来把容器里的null值剔除掉;

#import "NSDictionary+Check.h"
#import "NSArray+Check.h"

@implementation NSDictionary (Check)

- (NSDictionary*)removeEmptyValue {
    if(self){
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:self.count];
        
        for(NSString *keystr in self){
            id object = [self objectForKey:keystr];
            if(object == [NSNull null] || [object isKindOfClass:[UIImage class]]){
                //跳过
            }else if([object isKindOfClass:[NSDictionary class]]){
                NSDictionary *tmp = [object removeEmptyValue];
                [dict setObject:tmp forKey:keystr];
            }else if([object isKindOfClass:[NSArray class]]){
                NSArray *tmp = [object removeEmptyValueFromArray];
                [dict setObject:tmp forKey:keystr];
            }
            else{
                [dict setObject:object forKey:keystr];
            }
        }
        return dict;
    }
    else
        return [NSDictionary dictionary];
}



@end




#import "NSArray+Check.h"
#import "NSDictionary+Check.h"

@implementation NSArray (Check)

-(NSArray *)removeEmptyValueFromArray {
    NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity:self.count];
    for(id object in self){
        if(object == [NSNull null]  || [object isKindOfClass:[UIImage class]]){
            //跳过
        }else if([object isKindOfClass:[NSDictionary class]]){
            NSDictionary *tmp = [object removeEmptyValue];
            [tmpArray addObject:tmp];
        }else if([object isKindOfClass:[NSArray class]]){
            NSArray *tmp = [object removeEmptyValueFromArray];
            [tmpArray addObject:tmp];
        }else{
            [tmpArray addObject:object];
        }
    }
    return tmpArray;
}

@end