我试图理解如何使用函数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> ;,这意味着
你必须传递一个ObjCBool变量的地址。例:
你必须传递一个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 }