本文介绍了NSFileManager fileExistsAtPath:isDirectory 和 swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图了解如何在 Swift 中使用 fileExistsAtPath:isDirectory:
函数,但我完全迷失了.
I'm trying to understand how to use the function fileExistsAtPath:isDirectory:
with Swift but I get completely lost.
这是我的代码示例:
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
怎么办?
I can't understand how can I access the value for the b
MutablePointer. What If I want to know if it set to YES
or NO
?
推荐答案
第二个参数的类型为UnsafeMutablePointer
,这意味着您必须传递 ObjCBool
变量的 address.示例:
The second parameter has the type UnsafeMutablePointer<ObjCBool>
, which means thatyou have to pass the address of an ObjCBool
variable. Example:
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 和 Swift 4 的更新:
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
}
这篇关于NSFileManager fileExistsAtPath:isDirectory 和 swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!