如何在plist文件中编辑特定的NSDictionary?

时间:2013-03-25 21:13:06

标签: iphone ios ios6 nsdictionary

我在plist文件中有20个字典。我需要用他们的名字编辑一个特定的词典,但我真的不知道如何做到这一点。 我需要获取值并对SearchCountry Dictionary进行更改。

如何编辑特定字典值?

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试在属性列表中使用NSPorpertyListSerialization进行复杂迭代。

否则,您可能尝试使用以下代码访问特定字典

NSString*  plistPath = [[NSBundle mainBundle] pathForResource:@"yourPList" 
                                                     ofType:@"plist"];
NSDictionary* plistDict = [NSDictionary dictionaryWithContentsOfFile:plistPath]; 
// plistDict contains all the dictionaries inside it now, you could try to access the particular inside the plistDict

id *searchKey = [plistDict objectForKey:@"SearchCountry"]; 
//OR
NSString* searchKeys = [plistDict valueForKeyPath:@"SearchCountry"];

答案 1 :(得分:1)

获取这样的子词典:

NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilename];
NSMutableDictionary *searchCountry = [plist objectForKey:@"SearchCountry"];

更改其中一个值:

 [searchCountry setValue:@"Cleveland" forKey:@"searchcity"];

像这样保存:

BOOL success = [plist writeToFile:plistFilename atomically:YES];