Search code examples
swiftstacklldbxcode11

Change an Int variable value in LLDB


Environment: Xcode 11.3.1/Swift 5

Here is the function:

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")
    }
}

I debug before enter the if statement, and I change switchInt to 2 in lldb

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

but it still print "switchInt == 1" result


Solution

  • I guess the behavior is because the compiler has already evaluated the if statement "if switchInt == 1" because there's no code that changes the value of switchInt before that line. I tried the below and was able to get the behavior desired.

    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")
    }
    

    Now execute the command p switchInt and it will have the value 2. Step through the breakpoint and it will print switchInt == 2.