【问题标题】:writing an NSArray to plist works in simulator but fails in the device将 NSArray 写入 plist 在模拟器中有效,但在设备中失败
【发布时间】:2010-12-27 10:30:50
【问题描述】:

我正在尝试将 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");
    }

}

我认为路径有问题,但我不确定。

任何人都知道为什么会发生这种情况?谢谢

【问题讨论】:

    标签: iphone nsarray plist


    【解决方案1】:

    您不能写入设备上的应用程序包,您应该写入 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]];
    

    【讨论】:

    • 谢谢,但现在我如何通过 xCode 在该目录中创建 plist?因为每次我阅读它时,我都会阅读第一个创建的 plist 并且那个没有改变。非常感谢
    • 添加初始 plist 作为资源,然后在启动时检查 plist 文件是否存在于 docs 文件夹中然后读取它,如果它不存在则读取那个,它在应用程序包资源中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多