如何判断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类参考,没有找到任何适合我的任务.

有什么提示吗?

推荐答案

if (![fileManager fileExistsAtPath:[storeURL path]]) 
...

documentation人中:

如果此URL对象包含文件URL(由isFileURL确定),

Objective-c相关问答推荐

如何以编程方式获取iphone的IP地址

应用程序终止时处理推送通知

在 Objective-C 中定义协议的类别?

用 UIActivityIndi​​catorView 替换 UIBarButtonItem

在objective-c / cocoa-touch中是否有一个方便的功能来找到最小的数字?

改变 setContentOffset:animated: 的速度?

BSXPCMessage 收到消息错误:连接中断

NSManagedObjectContext performBlockAndWait:不在后台线程上执行?

iOS 在设备旋转时更改自动布局约束

Grand Central Dispatch (GCD) 与 performSelector - 需要更好的解释

无法加载从笔尖引用的图像

如何在 iPhone 中将 NSData 转换为字节数组?

在不导入自己的情况下播放系统声音

iOS:如何将 UIViewAnimationCurve 转换为 UIViewAnimationOptions?

首次提交应用内购买以供审核

从 UIImage 获取 Exif 数据 - UIImagePickerController

为什么 detailTextLabel 不可见?

如何获得设备的比例因子?

Xcode 警告未使用属性访问结果 - getter 不应用于副作用

UICollectionView:如何检测滚动何时停止