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. 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]){
开发者_StackOverflow 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:
// Write file to docs folder
NSString* docFolder = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * path = [docFolder stringByAppendingPathComponent:
[NSString stringWithFormat:@"chapter%d.plist",idChapter]];
精彩评论