在 iphone 上写一个字典到 plist
我想在现有的plist文件中添加一个新的字典,我该怎么做,我可以从文件中读取,但是用同样的方法,写入不起作用。我的 plist 结构如下所示:
Root Array
|__item0 Dictionary
|__item1 Dictionary
|__item2 Dictionary
写入文件的代码如下所示:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"list.plist"];
if (![fileManager fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}
NSMutableArray *dataRoot = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:@"value1" forKey:@"1"];
[item setObject:@"value2" forKey:@"2"];
[item setObject:@"value3" forKey:@"3"];
[dataRoot addObject:item];
[dataRoot writeToFile:plistPath atomically:YES];
[item release];
谁能帮我解决这个问题,谢谢!
我已经更新了我的代码,但它仍然不起作用...问题是 list.plist 文件没有复制到 ~/Documents 目录。
I want to add a new dictionary to the existing plist file, how can I do that, I can read from the file, but with the same approach, writing doesn't work. My plist structure looks like this:
Root Array
|__item0 Dictionary
|__item1 Dictionary
|__item2 Dictionary
The code of writing to the file looks like this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"list.plist"];
if (![fileManager fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}
NSMutableArray *dataRoot = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:@"value1" forKey:@"1"];
[item setObject:@"value2" forKey:@"2"];
[item setObject:@"value3" forKey:@"3"];
[dataRoot addObject:item];
[dataRoot writeToFile:plistPath atomically:YES];
[item release];
Can anyone help me with this, thanks!
I have updated my code, but it still doesn't work...And the problem is list.plist file doesn't get copied to ~/Documents directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你正在尝试编写主包,这在 iOS 上是不允许的。请阅读一些重要的应用程序目录。
但您的编码可归结为以下内容:
You are trying to write the main bundle which is no-no on iOS. Please read the A Few Important Application Directories.
But your coding boils down to the following:
我认为代码没有错误。
您检查过 plistPath、dataRoot 和 item 吗?
它们有正确的价值吗?
I think code is not wrong.
Have you check plistPath, dataRoot, and item?
They have correct value?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
默认情况下只能保存原始类型。
试试这个链接。
http://deadpanic.com/howtosave
他解释了如何使用 NSCoding 协议将任何对象保存到磁盘。
如果您只使用基元,那么我很抱歉浪费您的时间。但这是我将自定义内容写入磁盘所采用的路径。我认为这只是一个数组问题。
编辑:
我刚刚注意到...看起来您正在尝试写入主包中的数组。这些文件是只读的。
您需要在文档目录中保存文件的副本。
尝试找到文档目录,
将您的 plist 放入此文件夹中,然后您可以写入它。
You can only save primitive types by default.
try out this link.
http://deadpanic.com/howtosave
He explains how to save any object to disk by using NSCoding protocol.
If you are working only with primitives then I apologize for wasting your time. But this is the path I took to write my custom stuff to disk. And I thought it was just an array issue.
Edit:
I just noticed... It looks like you are trying to write to an array in the Main Bundle. These files are read only.
You need to save a copy of the file in the Documents directory.
try this out to find the documents directory
put your plist in this folder and you can write to it.