将 NSDictionary 保存到 plist [英] Save NSDictionary to plist

查看:26
本文介绍了将 NSDictionary 保存到 plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这篇文章中的代码:来自 Plist 的多个数组,使用相同的 plist 格式.

I am using the code found in this post: Mulitple Arrays From Plist, with the same plist formatting.

这很成功,但是我不知道如何以相同的格式将创建的数组保存回 plist.

This works successfully, however I do not know how to save the created arrays back into a plist, in the same format.

我将如何实现这一目标?

How would I achieve this?

我需要的不是保存,而是要保存的数据的形成.
plist 是一个包含字符串及其对应键的字典数组.所有具有特定键的字符串都放入自己的数组中.

It is not so much the saving that I need, but the forming of the data to save.
The plist is an array of dictionaries with strings and their corresponding keys. All the strings with a certain key are put into an array of their own.

如何将该数组放回字典数组中的正确位置,准备保存?

How would I put that array back into the correct positions in the array of dictionaries, ready to save?

推荐答案

不要忘记创建 myPlistFile.plist 并将其添加到您的应用程序资源文件夹中.

Don't forget to create myPlistFile.plist and add it in your application resource folder.

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

你可以在这里扫描路径并使用for循环搜索myPlistFile.plist.

you could scan paths here and search myPlistFile.plist using for loop.

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath: plistPath])
{
          NSString *bundle = [[NSBundle mainBundle] pathForResource:@"myPlistFile" ofType:@"plist"];
          [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:&error];
}
[myDict writeToFile:plistPath atomically: YES];

这篇关于将 NSDictionary 保存到 plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆