IOS 中会发生crash的操作
对字典和数组进行下列操作时会产生crash:
对于字典来说:
查询时,key=nil 或者 key=null 时都能正常运行
插入时,,key=nil 或者 key=null 都会crash
对于数组来说
index越界会crash
添加对象为nil时会crash
NSDictionary *dic = @{@"key":@"value"};
NSString *value = [dic objectForKey:nil];
value = [dic objectForKey:[NSNull null]];
NSMutableDictionary *mutDic = [dic copy];
value = [mutDic objectForKey:nil];
value = [mutDic objectForKey:[NSNull null]];
value = [mutDic objectForKey:[NSNull null]];
// [mutDic setObject:@"123" forKey:nil];crash
// [mutDic setObject:nil forKey:@"123"];crash
// [mutDic setObject:[NSNull null] forKey:@"key"];crash
// [mutDic setObject:@"value" forKey:[NSNull null]];crash
// [mutDic removeObjectForKey:nil]; //crash
// [mutDic removeObjectForKey:[NSNull null]];//crash
//
NSArray *arr = @[@(1),@(2),@(3)];
// value = [arr objectAtIndex:4];//crash
NSMutableArray *mutArr = [arr mutableCopy];
// [mutArr insertObject:@(5) atIndex:5];//crash
// [mutArr removeObjectAtIndex:5]; //crash
// [mutArr replaceObjectAtIndex:5 withObject:@"5"];//crash
// [mutArr insertObject:nil atIndex:0]; //crash
[mutArr insertObject:[NSNull null] atIndex:0];
[mutArr replaceObjectAtIndex:0 withObject:[NSNull null]];