iOS------[__NSDictionaryI removeObjectForKey:]

现象1

NSMutableDictionary*dict = [[NSMutableDictionary alloc]initWithDictionary:@{@"type":@"",@"START_NO":@"",@"END_NO":@"",@"SUM":@"",@"DOC_TYPE":@"",@"CCY":@"",@"FLAG":@""}];

NSArraylista =[[NSArray alloc]initWithObjects:dict, nil];
for (NSMutableDictionary
dict in lista) {

    [dict removeObjectForKey:@"type"];
}
NSLog(@"%@",lista);

输出为:
(
{
CCY = "";
"DOC_TYPE" = "";
"END_NO" = "";
FLAG = "";
"START_NO" = "";
SUM = "";
}
)

现象2:
NSMutableDictionarydict = [[NSMutableDictionary alloc]initWithDictionary:@{@"type":@"",@"START_NO":@"",@"END_NO":@"",@"SUM":@"",@"DOC_TYPE":@"",@"CCY":@"",@"FLAG":@""}];
NSMutableArray
lista =[[NSMutableArray alloc]init];
[lista addObject:dict];

for (NSMutableDictionary*dict in lista) {
    
    [dict removeObjectForKey:@"type"];
}
NSLog(@"%@",lista);

(
{
CCY = "";
"DOC_TYPE" = "";
"END_NO" = "";
FLAG = "";
"START_NO" = "";
SUM = "";
}
)

总结:
不管数组是可变或者是不可变的,只要数组内的字典为可变的,那就可以删除字典内的元素,以for in 顺便也将数组中字典的删除。

现象3:
NSDictionarydict = @{@"type":@"",@"START_NO":@"",@"END_NO":@"",@"SUM":@"",@"DOC_TYPE":@"",@"CCY":@"",@"FLAG":@""};
NSMutableArray
lista =[[NSMutableArray alloc]init];
[lista addObject:dict];

for (NSMutableDictionary*dict in lista) {
    
    [dict removeObjectForKey:@"type"];
}
NSLog(@"%@",lista);

结果:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI removeObjectForKey:]: unrecognized selector sent to instance 0x600003ad0d20'

总结:不管数组是可变还是不可变的,如果数组内的字典为不可变的,NSMutableDictionary*dict in lista的字典还是不可变的,切记。

推荐阅读更多精彩内容