将plist复制到文档目录 [英] copying plist to document directory

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

问题描述

如何将我在app构建中的现有plist复制到Document目录?

How do I copy an existing plist I have in the app build to the Document directory?

我的plist基本上是一个字典数组,我已经成功复制了使用以下内容链接到目录:

My plist is basically an array of dictionary, I have successfully copy the plist to the directory using the following:

 BOOL success;
            NSError *error;

            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"States.plist"];

            success = [fileManager fileExistsAtPath:filePath];
            if (success) return;

            NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];    
            success = [fileManager copyItemAtPath:path toPath:filePath error:&error];

            if (!success) {
                NSAssert1(0, @"Failed to copy Plist. Error %@", [error localizedDescription]);
            } 

然而,当我尝试访问NSDictionary时,它给了我:

However, when I try to access the NSDictionary it gives me:

CoreAnimation: ignoring exception: -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

这是为什么?我必须重新启动应用程序,现在我可以更改存储在我的数组中的字典

Why is this? I have to relaunch the app again and now I can change the dictionary stored in my array

推荐答案

只需创建一个新的pList文档目录,并设置为您拥有的现有plist的内容。

Just create a new pList in the documents directory, and set to the contents of the existing plist you have.

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

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