将数据写入plist

发布于 2024-11-10 13:08:53 字数 278 浏览 1 评论 0原文

我想从 plist 读取数据,添加一些元素并将数据写入 plist。(更新 plist)。

我想让 plist 保存一个字典数组,将该数组读入我的应用程序,添加字典,然后将数组写回 plist。

这是怎么做到的?我也不确定在应用程序首次启动时在哪里以及如何创建 plist。还是应该从一开始就放在捆绑包中?

我不确定的另一件事是 plist 字典是否可以将例如 CLLocations 作为键的值?它是否会正确保存,或者 CLLocation 是否需要分解为纬度和经度值?

谢谢

I would like to read data from a plist, add some elements and the write the data to a plist.(update a plist).

I want to have the plist hold an array of dictionaries, read this array into my app, add dictionaries and then write the array back to the plist.

How is this done? Im also not sure where and how to create the plist on the apps first launch. or should it be placed in the bundle from the beginning?

Another thing I am not sure about is if the plist dictionaries can hold e.g. CLLocations as values for keys? Will it be saved correctly or would the CLLocation need to be broken into lat and lon values?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-11-17 13:08:53

从 plist 创建字典的最简单方法是使用方法 dictionaryWithContentsOfFile:,例如,从资源中加载 plist:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

编写 plist 同样简单:(

[dict writeToFile:filePath atomically:YES];

请注意,您不能写入应用程序的资源,因此您必须创建不同的路径,例如在文档目录中,请参阅NSSearchPathForDirectoriesInDomains

Plists只能包含以下实例NSDictionaryNSArrayNSNumberNSString,因此您不能放置 CLLocation > 在 Plist 中,无需转换。如果您需要保存非 Plist 对象,请查看 NSKeyedArchiver。它可以从任何采用 NSCoding 协议的东西创建文件和 NSData 对象(CLLocation 可以)。

The easiest way to create a dictionary from a plist is to use the method dictionaryWithContentsOfFile:, for example, to load a plist from your resources:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

Writing a plist is equally simple:

[dict writeToFile:filePath atomically:YES];

(Note that you can't write into your app's resources, so you'll have to create a different path, e.g. in your Documents directory, see NSSearchPathForDirectoriesInDomains)

Plists can only contain instances of NSDictionary, NSArray, NSNumber and NSString, so you can't put a CLLocation in a Plist without conversion. If you need to save non-Plist objects, have a look at NSKeyedArchiver. It can create files and NSData objects from anything that adopts the NSCoding protocol (CLLocation does).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文