将数据写入plist
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 plist 创建字典的最简单方法是使用方法
dictionaryWithContentsOfFile:
,例如,从资源中加载 plist:编写 plist 同样简单:(
请注意,您不能写入应用程序的资源,因此您必须创建不同的路径,例如在文档目录中,请参阅
NSSearchPathForDirectoriesInDomains
)Plists只能包含以下实例
NSDictionary
、NSArray
、NSNumber
和NSString
,因此您不能放置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:Writing a plist is equally simple:
(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
andNSString
, so you can't put aCLLocation
in a Plist without conversion. If you need to save non-Plist objects, have a look atNSKeyedArchiver
. It can create files andNSData
objects from anything that adopts theNSCoding
protocol (CLLocation
does).