NSFileManager fileExistsAtPath:isDirectory 和 swift [英] NSFileManager fileExistsAtPath:isDirectory and swift

查看:28
本文介绍了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 的值.如果我想知道它是否设置为 YESNO 怎么办?

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 that you 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆