检查文件是否存在于 URL 而不是路径 [英] Check if file exists at URL instead of path

查看:48
本文介绍了检查文件是否存在于 URL 而不是路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查 URL(而不是路径)中是否存在文件,以便将预先填充的默认存储设置到 iPhone 模拟器中:

NSURL *storeURL = [[self applicationDocumentsDirectory] ​​URLByAppendingPathComponent:@"Food.sqlite"];/*设置商店.为了说明起见,提供一个预先填充的默认存储.*/NSFileManager *fileManager = [NSFileManager defaultManager];//如果预期的商店不存在,则复制默认商店.if (![fileManager fileExistsAtPath:storePath]) {NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Food" ofType:@"sqlite"];如果(默认存储路径){[fileManager copyItemAtPath:defaultStorePath toPath:storePath 错误:NULL];}}

我读到在最近版本的模板中,applicationDocumentsDirectory 方法返回一个 URL,因此我更改了代码以使用 NSURL 对象来表示文件路径.但是在 [fileManager fileExistsAtPath:storePath] 处,我需要将 fileExistsAtPath 更改为类似 fileExistsAtURL 的内容(显然它不存在).

我检查了 NSFileManager 类参考,但没有找到适合我的目的的任何合适的任务.

有什么提示吗?

解决方案

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

来自文档:

<块引用>

如果此 URL 对象包含文件 URL(由 isFileURL 确定),此方法的返回值适用于输入到NSFileManager 或 NSPathUtilities.如果路径有一个斜杠被剥光了.

How can I check if a file exists at a URL (instead of a path), in order to set a pre-populated default store into the iPhone Simulator:

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];
    }
}

I've read that in recent versions of the templates, the applicationDocumentsDirectory method returns an URL, so I've changed the code to use NSURL objects to represent the file path. But at [fileManager fileExistsAtPath:storePath], I need to change fileExistsAtPath to something like fileExistsAtURL (obviously it doesn't exist).

I've checked the NSFileManager Class Reference and I didn't find any suitable task suited for my purpose.

Any hints please?

解决方案

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

From the documentation:

If this URL object contains a file URL (as determined with isFileURL), the return value of this method is suitable for input into methods of NSFileManager or NSPathUtilities. If the path has a trailing slash it is stripped.

这篇关于检查文件是否存在于 URL 而不是路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆