Swift 中 - fileExistsAtPath:isDirectory: 方法的使用


一般我们用- fileExistsAtPath:来判断一个文件是否存在,但是如果想同时判断这个路径是否为目录的话可以用- fileExistsAtPath:isDirectory:,在Swift中如何使用请看下面的代码:

var directory: ObjCBool = ObjCBool(false)
var exists: Bool = NSFileManager.defaultManager().fileExistsAtPath("…", isDirectory: &directory)

if exists && Bool(directory) {
    // Exists. Directory.
} else if exists {
    // Exists.
}

Swift 3.0 中的写法如下:

let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: fullPath, isDirectory:&isDir) {
    if isDir.boolValue {
        // file exists and is a directory
    } else {
        // file exists and is not a directory
    }
} else {
    // file does not exist
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容