美文网首页
Swift 中 - fileExistsAtPath:isDir

Swift 中 - fileExistsAtPath:isDir

作者: TomatosX | 来源:发表于2016-04-06 16:37 被阅读2419次

一般我们用- 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
}

相关文章

网友评论

      本文标题:Swift 中 - fileExistsAtPath:isDir

      本文链接:https://www.haomeiwen.com/subject/ivlmlttx.html