更改LLDB中的Int变量值

问题描述 投票:3回答:2

环境: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

swift stack lldb xcode11
2个回答
4
投票

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


0
投票

在Swift中从调试器设置变量有些麻烦。因为swift使用了很多包装的对象(例如,Int实际上是一个“结构”),所以即使在-Onone时,编译器也必须进行大量的优化,否则代码将运行缓慢。

通常仅告诉调试器有关变量的卷影副本,而不告知代码中实际使用的位置。您可以尝试费利克斯(Felix)建议的各种技巧,但目前不能保证会成功...

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

© www.soinside.com 2019 - 2024. All rights reserved.