开发者

iPhone - Saving and Retrieving Data

I am having trouble determining if a file exists in the documents folder. Even if I haven't save any data to a file with an NSKeyedArchiver, my test returns true. The test:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"gameData.dat"];

if ([fileManager fileExistsAtPath:documentPath]) {
    //if the file exists retrieve the data.
  开发者_C百科  //this is always called, even if there is no file at the documentPath.
}else{
    //set the defaults and save data.
}


You sure that there isn't a file there already? When I just run your snippet in an empty app on simulator, it's not finding file exists. Putting an empty gameData.dat file into that dir on my Mac and running it on the simulator now finds a file (run it on the simulator, but a break point in there and look at the full path and goto that dir in a shell and try it).


Did this file exist in a preview iteration of the app? If you loaded the app on the phone at an earlier point in time with this file in existence, if you don't delete the app before you load a new version, it will still find that old file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜