我有一个问题,我创建了一个电影列表。Root的类型是Dictionary,Pixar的类型是Array,有3个字符串电影标题,我可以用以下代码从列表中读取,没有问题
NSString *path = [[NSBundle mainBundle] pathForResource:@"Movies" ofType:@"plist"];
NSMutableDictionary *movies =[[NSMutableDictionary alloc] initWithContentsOfFile:path];从这里我可以打印列表,没有问题。现在我想将另一部电影添加到此列表中。因此,我将列表转换为数组,然后将数组保存回文件中,但它不起作用。知道我哪里做错了吗?
NSMutableArray *array= [movies valueForKey:@"Pixar"];
NSString *incredible=@"Incredibles";
[array addObject:incredible];
[movies setObject:array forKey:@"Pixar"];
[movies writeToFile:path atomically:YES];发布于 2013-06-04 05:32:09
您不能在设备上写入捆绑包。模拟器不会强制执行这些约束,但设备会强制执行。
一种方法是:
Documents文件夹中是否存在该文件。Documents文件夹中读取该文件。如果没有,请从捆绑包中读取。Documents文件夹。因此:
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Movies" ofType:@"plist"];
NSString *docsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *documentsPath = [docsFolder stringByAppendingPathComponent:@"Movies.plist"];
NSMutableDictionary *movies = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:documentsPath])
movies = [NSMutableDictionary dictionaryWithContentsOfFile:documentsPath];
if (!movies)
movies = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
// do whatever edits you want
[movies writeToFile:documentsPath atomically:YES];https://stackoverflow.com/questions/16905805
复制相似问题