更改 LLDB 中的 Int 变量值

use*_*561 4 stack lldb swift xcode11

环境?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")
    }
}
Run Code Online (Sandbox Code Playgroud)

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

e switchInt = 2
p switchInt
(Int) $R4 = 2
Run Code Online (Sandbox Code Playgroud)

但它仍然打印“switchInt == 1” 结果

Fel*_*gam 5

我猜这种行为是因为编译器已经评估了 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")
}
Run Code Online (Sandbox Code Playgroud)

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