更改 LLDB 中的 Int 变量值

Change an Int variable value in LLDB

环境: Xcode 11.3.1/Swift 5

函数如下:

func lldbTest() {
    var switchInt = 1

    ...// do something and set a break point here

    if switchInt == 1 {
        print("switchInt == 1")
    } else if switchInt == 2 {
        print("switchInt == 2")
    }
}

我在进入if语句之前调试,我在lldb中将switchInt改为2

e switchInt = 2
p switchInt
(Int) $R4 = 2

但它仍然打印 "switchInt == 1" result

我猜这种行为是因为编译器已经评估了 if 语句 "if switchInt == 1",因为在该行之前没有代码更改 switchInt 的值。我尝试了以下并能够获得所需的行为。

var switchInt = 1

for i in 0..<10 {
    switchInt = 0
}

if switchInt == 1 {  -> Put a break point here and use (lldb) e switchInt=2
    print("switchInt == 1")
} else if switchInt == 2 {
    print("switchInt == 2")
}

现在执行命令 p switchInt,它的值为 2。通过断点,它会打印 switchInt == 2。

在 Swift 中从调试器设置变量有点碰运气。因为 swift 使用了如此多的包装对象(例如 Int 实际上是 "struct"),编译器即使在 -Onone 时也必须进行大量优化,否则代码将 运行 慢得令人无法接受.

调试器通常只被告知变量的卷影副本,而不是代码中实际使用的位置。你可以尝试像Felix建议的各种技巧,但目前你不能保证成功...

这是一个已知错误,但由于技术原因,很难解决。