如何以编程方式编辑plist?

时间:2016-09-08 17:37:25

标签: objective-c nsarray nsdictionary plist

我有一个编辑plist文件的代码。但是当代码运行时,它会更改plist文件,但会删除其他一些字典。您可以查看图像以了解我的意思。

要查看已编辑的单词,请查看字典“第1项”和字符串“name”。您将看到它需要从“Second”更改为“newVALUE”。

原始plist 是plist创建时的plist图像。 然后你有 期望的plist ,这就是plist应该是什么样子。应用代码后, 编辑的plist 就是plist。

这是代码:

NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
plistPath = [plistPath stringByAppendingPathComponent:@"PassaveData.plist"];

NSMutableArray*  newContent = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
NSMutableDictionary *Dict = [[NSMutableDictionary alloc]initWithDictionary:[newContent objectAtIndex:1]];
[Dict setValue:@"newVALUE" forKey:@"name"];
[Dict writeToFile:plistPath atomically:YES];

这些是图片

Click to see the images

1 个答案:

答案 0 :(得分:1)

您的原始plist包含一系列词典。您创建一个新的字典,然后只用一个新字典覆盖原始的基于数组的plist。

您需要使用更新的字典更新已加载的数组,然后写出整个更新的数组。

@SpringBootTest(properties = {"some.username=user", "some.password=pwd"},
                webEnvironment = SpringBootTest.WebEnvironment.NONE)

请注意对数组和字典使用现代语法。