简体   繁体   English

将NSArray写入plist可以在模拟器中运行,但无法在设备中运行

[英]writing an NSArray to plist works in simulator but fails in the device

I'm trying to save an NSArray to a plist, when i try it on the simulator, it works properly but if i run it on the device, it fails at writting. 我正在尝试将NSArray保存到plist,当我在模拟器上尝试它时,它可以正常工作,但是如果我在设备上运行它,则书写失败。 here is the code: 这是代码:

-(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");
    }

}

I suppose that something is wrong with the path but i'm not sure. 我想路径有些问题,但我不确定。

anyone knows why can be this happening? 谁知道为什么会这样? thanks 谢谢

You can't write to application bundle on device, you should write to Documents folder or Caches folder instead: 您无法写入设备上的应用程序捆绑包,而应写入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]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM