【发布时间】:2011-12-14 07:07:11
【问题描述】:
如何检查文件是否存在于 URL(而不是路径)中,以便将预填充的默认存储设置到 iPhone 模拟器中:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Food.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Food" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
我已经读过,在最近的模板版本中,applicationDocumentsDirectory 方法返回一个 URL,因此我更改了代码以使用 NSURL 对象来表示文件路径。但是在[fileManager fileExistsAtPath:storePath],我需要将fileExistsAtPath 更改为fileExistsAtURL 之类的东西(显然它不存在)。
我检查了 NSFileManager 类参考,但没有找到适合我目的的合适任务。
有什么提示吗?
【问题讨论】: