编辑NSMutableDictionary子词典 [英] Editing an NSMutableDictionary sub-dictionary
本文介绍了编辑NSMutableDictionary子词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些由Json
文件制成的NSMutableDictionary.
I have some NSMutableDictionary that is made from Json
file.
NSMutableDictionary *result=//from json string
than,我需要编辑result
,其构建方式如下:
than , i need to edit that result
,which is built like :
{
slots = (
{
capacity = 1;
slot = sybIWQGDWw;
taken = (
"11:45-12:45",
"12:45-17:45"
);
},
{
capacity = 1;
slot = WNySjEZAmU;
taken = (
"12:00-13:00",
"13:00-18:00"
);
}
);
}
要获取我拥有的所有字段的数组,
To get array of all fields i have :
NSMutableArray *slots=[result objectForKey:@"slots"];
比我得到的所有词典都多:
than i get all dictionaries with :
for(NSMutableDictionary *dic in slots)
[dic setObject:@"1" forKey:@"slot"];//crash probably because i edit in for loop
现在我在尝试更改for循环中的字段时崩溃.
Now i get crash when trying to change a field in the for loop.
如何更改某个字段?
编辑
这就是我从json制作字典的方法(这比可变副本要容易!)
This is how i make the dictionary from json (which is than mutable copy to result! )
-(NSDictionary*)getDictionaryForJsonWithString:(NSString*)string
{
NSString *dataString=string;
NSData *myData=[dataString dataUsingEncoding:NSASCIIStringEncoding];
NSError *error;
NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:&error];
//NSLog(@"getDictionaryForJsonWithString: data is: %@",dataDic);
return dataDic;
}
NSMutableDictionary *result=[[self getDictionaryForJsonWithString:json] mutableCopy];
推荐答案
从JSON创建字典时,您需要设置创建可变实例的选项,并且它可能既适用于容器又适用于叶子:
When you create the dictionary from JSON, you need to set the option to create mutable instances, and it should probably be for both containers and leaves:
option: NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves
这篇关于编辑NSMutableDictionary子词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文