微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在调试器,控制台和文件中查看可选项

如何解决在调试器,控制台和文件中查看可选项

我在Swift中有以下课程:

let kKey_DirectoryURL   = "DirectoryURL"
let kKey_TempLogURL     = "TempLogURL"
let kKey_Version        = "Version"

class LogModel: NSObject,NSSecureCoding {
    var version = 1
    var url: URL? = nil
    var tempLog: URL? = nil
    
    static var supportsSecureCoding = true
    
    override init() {
        url = FileManager.default.temporaryDirectory
    }
    
    required init?(coder: NSCoder) {
        version = coder.decodeInteger(forKey: kKey_Version)
        url = coder.decodeObject(forKey: kKey_DirectoryURL) as! URL?
        if coder.containsValue(forKey: kKey_TempLogURL) {
            tempLog = (coder.decodeObject(forKey: kKey_TempLogURL) as! URL)
        }
    }

    func encode(with coder: NSCoder) {
        coder.encode(version,forKey: kKey_Version)
        coder.encode(url,forKey: kKey_DirectoryURL)
        if tempLog != nil {
            coder.encode(tempLog,forKey: kKey_TempLogURL)
        }
    }
}

如果我进入override init()方法,将url设置为临时目录后,它将在变量窗格中将url显示nil

Xcode debugger's variable pane showing the url variable as nil

但是,如果我使用po命令在lldb中打印它,它将显示期望值:

lldb console printing the url variable and showing it's not nil

此外,如果我使用encode(with: )方法对数据进行编码并将其保存到磁盘,则会在文件数据中看到预期的值:

file:/// var / folders / k9 / x4kl1v914dz3r813p_flns1c0000gp / T / ...

但是当我尝试通过init?(coder: )读回文件时,结果在变量窗格和lldb控制台中均为nil

Reading the value in from disk sets it to nil also

为什么变量面板没有显示nil,却显示nil?保存正确后,为什么值从文件nil中返回?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。