我有一个 NSArray,它给了我以下数据:
01/14/2013 13:28:06.559 IUser Reader [71164: c07] (
{
"id_acompanhante" = "";
"id_evento" = 34;
"user_id" = 1;
"inserido_por" = "Himself";
name = iUser;
status = done;
type = User;
}
{
"id_acompanhante" = 1;
"id_evento" = 34;
"user_id" = 1;
"inserido_por" = iUser;
name = "Mayara Roca";
status = naofeito;
type = companion;
}
)
如何将此数据播放到 NSMutableArray 并在每个项目中添加另一个字段。例子:
{
"id_acompanhante" = "";
"id_evento" = 34;
"user_id" = 1;
"inserido_por" = "Himself";
name = IUser;
status = done;
type = User;
tag = 2;
}
我又添加了一个字段“TAG”。
我该怎么做呢?
最佳答案
NSMutableArray *mutableArray = [NSMutableArray array];
for (NSDictionary *dictionary in yourArray) {
NSMutableDictionary *mutDictionary = [dictionary mutableCopy];
[mutDictionary setObject:[NSNumber numberWithInt:2] forKey:@"tag"];
[mutableArray addObject:mutDictionary];
}
这就是如何做你问的。使数组可变将不允许您更改其中的字典内容。您需要拉出每个字典并使它可变,然后将其存储回数组中。