Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用NSDictionary对象写入plist文件_Iphone_Xcode_Plist_Nsdictionary - Fatal编程技术网

Iphone 使用NSDictionary对象写入plist文件

Iphone 使用NSDictionary对象写入plist文件,iphone,xcode,plist,nsdictionary,Iphone,Xcode,Plist,Nsdictionary,很抱歉,我看到了类似的问题,但他们似乎没有一些完整的答案给我。我努力把它整理好,这样人们就不会恨我或我糟糕的英语。 我正在使用带有故事板和ARC的Xcode 4.2 我可以读取我的plist文件。我的任务只是将更新后的值写回plist文件 我的plist包含在主文件夹的“支持文件”子文件夹中(故事板就是其中的内容)。文件名为call Global.plist,GlobalValue2是文件类型字符串的一个元素 读取文件部分如下所示 NSString *plistfile = [[NSBundle

很抱歉,我看到了类似的问题,但他们似乎没有一些完整的答案给我。我努力把它整理好,这样人们就不会恨我或我糟糕的英语。
我正在使用带有故事板和ARC的Xcode 4.2
我可以读取我的plist文件。我的任务只是将更新后的值写回plist文件

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

读取文件部分如下所示

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

FirstValueTextBox.text = [[dict valueForKey:@"GlobalValue1"] stringValue];
从一些方便的youtube视频中学习,效果很好。将值更新到“我的文本框”

当我写回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];
结果是,我确实看到第二个文本框中弹出了一个更新的值,但plist文件保持不变

下面是我的问题的分类和我对这个问题的猜测

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

  • 我使用NSMutableDictionary可以调用setValue。它不会使我崩溃,当我在“SecondValueTextBox.text=[dict valueForKey:@“GlobalValue2”];”调用更新后的值时,它确实会返回更新后的值。但是plist文件中的内容不会更改。这是我现在得到的结果。
    我猜:在到处搜索之后,我认为“支持文件”也是只读的。纯粹的猜测确实看到有人直接谈论这件事

  • 我确实试着继续讲下去,有些人谈到Xcode中的“文档文件夹”是一个读写的地方。我想人们也在谈论写一个代码来访问那个文件夹。有人能告诉我这里的代码吗

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


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

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

    以下是复制文件的方法:

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

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

    以下是复制文件的方法:

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

    对不起,如果这个问题对你们中的任何人来说都是直截了当的。NSDocumentDirectory或document文件夹(或tmp文件夹)1。只存在于运行时?2.仅在我必须部署的设备中定位。所以我不能在开发期间添加plist?或3。这是一个读写文件夹,但支持文件夹不是,所以我把它复制到那里?或者这背后还有其他原因??如果这个问题对你们中的任何人来说都是直截了当的话,我很抱歉。NSDocumentDirectory或document文件夹(或tmp文件夹)1。只存在于运行时?2.仅在我必须部署的设备中定位。所以我不能在开发期间添加plist?或3。这是一个读写文件夹,但支持文件夹不是,所以我把它复制到那里?或者背后还有其他原因??