使用NSDictionary对象写入plist文件 [英] write into plist file using NSDictionary object

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

问题描述

对不起,我看到了类似的问题,但他们似乎没有一些完整的答案。我试着把它整理好让人们不讨厌我或我的英语不好。

我正在使用Xcode 4.2和故事板和ARC

我可以从我的plist中读取文件。我的任务只是将更新的值写回我的plist文件。

Sorry I saw similar questions but they don't seem to have some full answers for me. And i try to put it in order so that people will not hate me or my poor english.
I am working with Xcode 4.2 with storyboard and ARC
I can read from my plist file. My task is simply to write back the updated value(s) to my plist file.

我的plist包含在主文件夹的支持文件子文件夹中(其中)事情就是这样。该文件调用Global.plist,GlobalValue2是文件类型字符串的元素。

My plist is contain in "supporting files" sub folder of the main folder (where story-board is things goes). the file is call Global.plist and GlobalValue2 is a element of the file type string.

所以读取文件部分看起来像这样

So the read file part looks like this

NSString *plistfile = [[NSBundle mainBundle] pathForResource:@"Global" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistfile];

FirstValueTextBox.text = [[dict valueForKey:@"GlobalValue1"] stringValue];

从一些方便的youtube视频中学习它就可以了。将值更新到我的文本框。

learn it from some handy youtube video works just fine. updates the value to my text box.

当我回写我的plist文件时,真正的问题就出现了。当我尝试以下

The real problem comes in when I write back my plist file. When i try the following

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

[dict setValue:@"ABC" forKey:@"GlobalValue2"];
SecondValueTextBox.text = [dict valueForKey:@"GlobalValue2"];
[dict writeToFile:plistfile atomically:YES];

结果是我真的看到第二个文本框中弹出更新的值,但plist文件维持不变。

the result is I really saw a updated value pop up on the second text box, but the plist file remain unchanged.

以下是我的问题的细分和我对问题的猜测

The following are the break down of my questions and my guess for the problem


  1. 我尝试使用NSDictionary(不是NSMutableDictionary)并调用setValue(在运行时崩溃)

    我的猜测:NSDictionary对象本身是只读的,所以当我说增加值时它会让我崩溃。但是为什么在编码时没有错误呢?如果对象是readonly

  1. I try to use NSDictionary(not NSMutableDictionary) and call setValue (crash in runtime)
    my guess: NSDictionary object itself is readonly so it crash me when i say add value. But why don't it error me when in coding time? if the object is readonly

我使用NSMutableDictionary可以调用setValue。当我在SecondValueTextBox.text = [dict valueForKey:@GlobalValue2]中调用更新值时,它不会让我崩溃;它确实给我更新了更新的价值。但plist文件内的内容不会更改。这是我现在的结果。

我的猜测:经过一些搜索后,我认为支持文件也是只读的。纯粹的猜测确实看到有人直接谈论它。

I use NSMutableDictionary can call setValue. it doesn't crash me and when i call the updated value at "SecondValueTextBox.text = [dict valueForKey:@"GlobalValue2"];" it really return me the updated value. but the content inside of the plist file is not changed. Which is the result I have right now.
my guess: after some search here and there I think "supporting files" is read only too. pure guess did see anyone directly talk about it.

我确实试图继续前进,有些人谈到Xcode中的一个文档文件夹,它是一个读写位置。我想人们还会谈论编写代码来访问该文件夹。有人可以在这里给我看代码。

I did try to move on a little more and some people talks about a "document folder" in Xcode that is a read and write place. I think people also talk about write a code to access that folder. Can someone show me the code here.

我的上一个问题,我可以将我的Xcode连接到文档文件夹或我在哪里可以看到它(真正的文件夹结构与内部不同) Xcode我想)。所以我可以看到和编辑我的plist文件进行测试,我可以看到真实的结果而不使用代码和东西

My last question, can I hook up my Xcode to that "document folder" or where can i see it(the real file folder structure is different from inside Xcode I think). So that i can see and edit my plist file for testing, and i can see the real result without using codes and stuff



<如果人们可以告诉我我的猜测是对还是错,以及对我的3和4问题的回答,我将不胜感激。

I will be much appreciated if people can tell me my guess is right or wrong and the answer to my 3 and 4 question.

推荐答案

为了让您的更改保留在plist中,您确实需要在启动应用程序时将其从资源包复制到文档集,然后使用文档中的plist进行读写。

In order for your changes to be persisted in your plist, you would indeed need ot copy it from the resource bundle to the documents dirtectory on launch of the application, then use the plist in the documents to read and write.

以下是复制文件的方法:

Here is how you can copy the file:

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

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Global.plist"];

if ([fileManager fileExistsAtPath:plistPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"Global" ofType:@"plist"];
    [fileManager copyItemAtPath:resourcePath toPath:plistPath error:&error];
}

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

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