我正在尝试将NSArray保存到plist,当我在模拟器上尝试它时,它工作正常,但如果我在设备上运行它,它在写入失败。这是代码:
-(void)writePlist:(NSArray*)_newLevelAr{
NSArray * levels = [NSArray arrayWithArray:_newLevelAr];
NSString *path = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat:@"chapter%d",idChapter] ofType:@"plist"];
if([levels writeToFile:path atomically: YES]){
NSLog(@"write succesful");}
else {
NSLog(@"write failed");
}
}
我认为路径有问题,但我不确定。
任何人都知道为什么会发生这种情况?感谢
答案 0 :(得分:3)
您无法在设备上写入应用程序包,您应该写入Documents文件夹或Caches文件夹:
// Write file to docs folder
NSString* docFolder = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * path = [docFolder stringByAppendingPathComponent:
[NSString stringWithFormat:@"chapter%d.plist",idChapter]];