gpt4 book ai didi

swift - 如何检查在 swift 中声明的类型为 UnsafeMutablePointer 的变量的值?

转载 作者:行者123 更新时间:2023-11-30 11:14:02 26 4
gpt4 key购买 nike

我需要检查 Swift 中类型为 UnsafeMutablePointer? 的函数返回的变量的值。

声明如下:

    var isDirectory: UnsafeMutablePointer<ObjCBool>?

当我打印出来时:

    print(isDirectory)

我得到:

nil

我不太擅长将 Swift 与 Objective-C 混合。任何帮助将不胜感激。

这是我实际的完整代码:

    let directoryURL: URL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

let pathOldLocalStorage: String = directoryURL.path + "/OldLocalStorage"

FileManager.default.createFile(atPath: pathOldLocalStorage, contents: nil, attributes: nil)

var isDirectory: UnsafeMutablePointer<ObjCBool>?

let fileExists: Bool = FileManager.default.fileExists(atPath: pathOldLocalStorage, isDirectory: isDirectory)

print("fileExists=", fileExists, "isDirectory=", isDirectory as Any)

这是调试窗口结果:

fileExists= true isDirectory= nil

最佳答案

你需要这样调用它:

var isDirectory: ObjCBool = false

let fileExists: Bool = FileManager.default.fileExists(atPath: pathOldLocalStorage, isDirectory: &isDirectory)

if fileExists {
print("isDirectory=\(isDirectory)")
}

关于swift - 如何检查在 swift 中声明的类型为 UnsafeMutablePointer<ObjCBool​​> 的变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51913655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com