4

运行以下代码时,[dict setValue:@"null" forKey:@"name"];不断崩溃。我在这里搜索,发现其他帖子是由不使用NSMutableDictionary. 但是我正在使用这个。

name如果是,为什么它会在这条线上崩溃null

NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
for (NSMutableDictionary *dict in [[json objectForKey:@"data"] mutableCopy]) {
    if ([dict objectForKey:@"name"] == [NSNull null]) {
        [dict setValue:@"null" forKey:@"name"];
    }
    [tempCustomers addObject:dict];
}
4

3 回答 3

15

我最终使用了这个。我猜这就是deepMutableCopy是什么?

NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
for (NSMutableDictionary *dict in [[json objectForKey:@"data"] mutableCopy]) {
    if ([dict objectForKey:@"name"] == [NSNull null]) {
        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
        tempDict = [dict mutableCopy];

        [tempDict setValue:@"null" forKey:@"name"];
        [tempCustomers addObject:tempDict];
    } else {
        [tempCustomers addObject:dict];
    }
}
于 2012-10-01T17:10:51.727 回答
2

也许 mutableCopy 不是“deepMutableCopy”,我的意思是,你只是枚举了可变对象,但是这个集合中的对象不是可变的(从我的评论中复制)

于 2012-10-01T17:01:25.127 回答
0

创建新数组[[NSMutableArray alloc]initWithArray:[json objectForKey:@"data"]]并在 for 循环中使用它

而是在[json objectForKey:@"data"]将其替换为新阵列时运行。它应该是这样的:

NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
NSMutableArray *search = [[NSMutableArray alloc]initWithArray:[json objectForKey:@"data"]];
for (NSMutableDictionary *dict in search)
{
    if ([dict objectForKey:@"name"] == [NSNull null])
    {
        [dict setValue:@"null" forKey:@"name"];
    }
    [tempCustomers addObject:dict];
}
于 2014-11-18T08:31:33.613 回答