加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

NSFileManager fileExistsAtPath:isDirectory和swift

发布时间:2020-12-14 06:04:41 所属栏目:百科 来源:网络整理
导读:我试图理解如何使用函数fileExistsAtPath:isDirectory:with Swift但我完全失去了。 这是我的代码示例: var b:CMutablePointerObjCBool?if (fileManager.fileExistsAtPath(fullPath,isDirectory:b! )){ // how can I use the "b" variable?! fileManager.c
我试图理解如何使用函数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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读