简体   繁体   中英

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.
    //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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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