首页 > 解决方案 > Xcode 问题?对象中的字典在变量检查器中将所有值显示为假,而值实际上是真的?

问题描述

class ViewController: UIViewController {

    struct TestStruct: Decodable {
        var dict: [String: Bool]?
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let json = """
        {"dict":{
            "test": true
        }}
        """.data(using: .utf8)!

        let decoder = JSONDecoder()
        let product = try? decoder.decode(TestStruct.self, from: json)
        print(product)
    }
}

使用这段代码,dict 字典的“test”值应该是 true。如果您在 print(product) 上放置断点(断点不在打印上,但我在屏幕截图中做了一个步骤),并查看左下角变量检查器中的变量值,

在此处输入图像描述

dict [字符串:布尔]?1 个键/值对 some [0] (key: String, value: Bool)
key String "test"
value Bool false

该值设置为假。

但是,当我们使用 po 访问该值时,该值似乎被正确设置为 true:

 Optional<TestStruct>
  ▿ some : TestStruct
    ▿ dict : Optional<Dictionary<String, Bool>>
      ▿ some : 1 element
        ▿ 0 : 2 elements
          - key : "test"
          - value : true

我们想知道这里发生了什么?为什么 po 产品返回好的值,因为它包含字典,而 GUI 变量检查器说它设置为 false。

我们将 Xcode 10.1 用于测试项目。

标签: swiftxcodelldb

解决方案


推荐阅读