ios 测试文件是否存在的快速方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1356701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:22:12  来源:igfitidea点击:

What's a quick way to test to see a file exists?

iosiphonecocoa-touchnsfilemanagerfile-manager

提问by mahboudz

I want to quickly check to see if a file exists in my iPhone app's Documents directory (or any path for that matter). I can enumerate through the directory's files, or I can try to open a specific file. What's the fastest way? I just need to know if the file is there or if it does not exist.

我想快速检查我的 iPhone 应用程序的 Documents 目录(或任何与此相关的路径)中是否存在文件。我可以枚举目录的文件,或者我可以尝试打开特定文件。最快的方法是什么?我只需要知道文件是否存在或不存在。

回答by rein

Swift v3:

斯威夫特 v3:

let fileExists = FileManager.default.fileExists(atPath: somePath)

let fileExists = FileManager.default.fileExists(atPath: somePath)

Thanks to Nikolay Suvandzhiev.

感谢Nikolay Suvandzhiev

Objective-C (Original):

Objective-C(原始):

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];