iPhone - 保存和检索数据

时间:2011-04-24 16:46:20

标签: iphone objective-c cocoa-touch

我无法确定文档文件夹中是否存在文件。即使我没有使用NSKeyedArchiver将任何数据保存到文件,我的测试返回true。 测试:

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

2 个答案:

答案 0 :(得分:0)

你确定那里还没有文件吗?当我在模拟器上的空应用程序中运行您的代码段时,它找不到文件存在。将一个空的gameData.dat文件放入我的Mac上的dir并在模拟器上运行它现在找到一个文件(在模拟器上运行它,但是在那里有一个断点并查看完整路径并转到shell中的dir和试试吧。

答案 1 :(得分:0)

此文件是否存在于应用的预览版中?如果您使用此文件在较早的时间点在手机上加载应用程序,如果您在加载新版本之前未删除该应用程序,它仍会找到该旧文件。

相关问题