xcode 使用 NSDictionary 对象写入 plist 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9433598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:25:57  来源:igfitidea点击:

write into plist file using NSDictionary object

iphonexcodeplistnsdictionary

提问by Raymond Wong

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.

抱歉,我看到了类似的问题,但他们似乎没有为我提供完整的答案。我试着把它整理好,这样人们就不会讨厌我或我糟糕的英语。
我正在使用带有情节提要和 ARC 的 Xcode 4.2,
我可以从我的 plist 文件中读取。我的任务只是将更新的值写回我的 plist 文件。

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.

我的 plist 包含在主文件夹的“支持文件”子文件夹中(故事板所在的位置)。该文件名为 Global.plist,GlobalValue2 是文件类型字符串的一个元素。

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

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

从一些方便的 youtube 视频中学习它,效果很好。将值更新到我的文本框。

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

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

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

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

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

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

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

  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

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

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

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

  1. 我尝试使用 NSDictionary(而不是 NSMutableDictionary)并调用 setValue(运行时崩溃)
    我的猜测:NSDictionary 对象本身是只读的,所以当我说添加值时它会使我崩溃。但是为什么在编码时它不会出错?如果对象是只读的

  2. 我用 NSMutableDictionary 可以调用 setValue。它不会让我崩溃,当我在“SecondValueTextBox.text = [dict valueForKey:@"GlobalValue2"];”处调用更新的值时 它真的返回给我更新的值。但是 plist 文件里面的内容没有改变。这就是我现在的结果。
    我的猜测:在这里和那里进行一些搜索后,我认为“支持文件”也是只读的。纯猜测确实看到有人直接谈论它。

  3. 我确实尝试继续前进一点,有些人谈论 Xcode 中的“文档文件夹”,它是一个读写位置。我认为人们也谈论编写代码来访问该文件夹。有人可以告诉我这里的代码。

  4. 我的最后一个问题,我可以将我的 Xcode 连接到那个“文档文件夹”,或者我在哪里可以看到它(真正的文件夹结构与我认为的 Xcode 内部不同)。这样我就可以查看和编辑我的 plist 文件进行测试,而且我可以在不使用代码和其他东西的情况下看到真实的结果

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.

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

回答by Mutix

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.

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

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