NSFileManager fileExistsAtPath:isDirectory和Swift

我试图了解如何使用函数fileExistsAtPath:isDirectory:与斯威夫特,但我完全失去了。

这是我的代码示例:

 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的值。 如果我想知道是否设置为YESNO

第二个参数的types为UnsafeMutablePointer<ObjCBool> ,这意味着你必须传递一个ObjCBoolvariables的地址 。 例:

 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 }