1

help me, please, I'm new to Swift.

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) var flag = false print(" current-> \(flag)") /** Add a breakpoint **/ if flag == true { print(" after -> true") }else { print(" after -> false") } } 

I want to use lldb to modify the value of 'flag', so,

(lldb) po flag false (lldb) expression flag = true (lldb) po flag true (lldb) continue 2018-11-24 23:57:05.552804+0800 test_swift_lldb[6806:384106] XPC connection interrupted Process 6806 resuming current-> false after -> false 

It doesn't seem to be useful. Please tell me how to use lldb, modify the bool value.

2
  • 1
    Hi, please enter you code as text and not as an external image. You are trying to use the debugger, correct? See if this answer helps with entering debugger commands: how-to-change-variables-value-while-debugging-with-llvm-in-xcode Commented Nov 24, 2018 at 7:59
  • @Bill Do you have any good ideas? I was frustrated by this problem... Commented Nov 25, 2018 at 0:50

1 Answer 1

1

First of all, it seems to be an issue between Swift and LLDB. I suspect that Swift is optimizing the var flag away into a register. There are a couple of other SO questions on a similar problem, for instance: Why is Xcode's Variables View's “Edit Value” not changing the variable value?. Interestingly, Xcode somehow works around this problem. The work-around seems to be to "trick" LLDB into recognizing the the variable is updated. I modified your code as follows:

// // main.swift // debug_example // import Foundation print("Hello, main") var flag = false var debugString = "abcd" if debugString.count == 0 { flag = true } print(" current-> \(flag)") /** Add a breakpoint **/ if flag == true { print(" after -> true") }else { print(" after -> false") } 

The following are my LLDB commands simplified (output indented):

lldb main breakpoint set --line 8 process launch Process 64052 launched: po flag false ex flag=true po flag true s Hello, World! Target 0: (main) stopped. po flag false ex flag=true po flag true thread continue Resuming thread current-> true after -> true 

Perhaps someone else can provide more information or some insight into how Xcode works around this.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, it works. But,when i am coding, I will not write those codes to 'trick' lldb, Because those codes have no meaning for the project...
I completely agree. I just cleaned out some older versions of llvm and related components. I have lldb --version showing: lldb-1000.11.38.2 Swift-4.2. Now everything works as you would expect; no tricks required. Check your versions of swiftc and lldb.
I am ashamed to ask you, My current xcode configuration is lldb --version showing: lldb-1000.11.38.2 Swift-4.2。 But When I run my initial code for this question, it was not work... What caused it? <code> override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) var flag = false print(" current-> (flag)") /** Add a breakpoint **/ if flag == true { print(" after -> true") }else { print(" after -> false") } }</code>
Perhaps backup a bit. Create a very, very simple project like the sample code above. If you have Xcode installed, try if from there. Xcode uses lldb. Next try it from the command line: swiftc -g main.swift followed by lldb main. Like I mentioned, when I removed old versions of lldb, it worked for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.