NSFileManager fileExistsAtPath:isDirectory和swift

前端之家收集整理的这篇文章主要介绍了NSFileManager fileExistsAtPath:isDirectory和swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图理解如何使用函数fileExistsAtPath:isDirectory:with Swift但我完全失去了。

这是我的代码示例:

var b:CMutablePointer<ObjCBool>?

if (fileManager.fileExistsAtPath(fullPath,isDirectory:b! )){
    // how can I use the "b" variable?!
    fileManager.createDirectoryAtURL(dirURL,withIntermediateDirectories: false,attributes: nil,error: nil)
}

我不明白如何访问b MutablePointer的值。如果我想知道它是否设置为YES或NO?

第二个参数的类型是UnsafeMutablePointer< ObjCBool​​&gt ;,这意味着
你必须传递一个ObjCBool​​变量的地址。例:
var isDir : ObjCBool = false
if fileManager.fileExistsAtPath(fullPath,isDirectory:&isDir) {
    if isDir {
        // file exists and is a directory
    } else {
        // file exists and is not a directory
    }
} else {
    // file does not exist
}

更新Swift 3(Xcode 8.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相关文章