首页 > 解决方案 > 更改 LLDB 中的 Int 变量值

问题描述

环境: 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” 结果

标签: swiftstacklldbxcode11

解决方案


我猜这种行为是因为编译器已经评估了 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。


推荐阅读