将数据写入plist [英] Write data to plist

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

问题描述

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

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

我想让plist容纳一个字典数组,将此数组读入我的应用程序,添加字典,然后将该数组写回到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.

这是怎么做的?我也不确定在首次启动时在何处以及如何在应用程序上创建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?

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

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?

谢谢

推荐答案

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

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];

编写plist同样简单:

Writing a plist is equally simple:

[dict writeToFile:filePath atomically:YES];

(请注意,您无法写入应用程序的资源,因此您必须创建其他路径,例如,在Documents目录中,请参见NSSearchPathForDirectoriesInDomains)

(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)

Plist只能包含NSDictionaryNSArrayNSNumberNSString的实例,因此您不能在没有转换的情况下将CLLocation放入Plist中.如果需要保存非Plist对象,请查看

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).

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

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