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

问题描述

我在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 (将#修改为@)