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.

  • 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](https://stackoverflow.com/questions/9907387/how-to-change-variables-value-while-debugging-with-llvm-in-xcode) – Bill Nov 24 '18 at 07:59
  • @Bill Do you have any good ideas? I was frustrated by this problem... – zw骑猪o0o找牛 Nov 25 '18 at 00:50

1 Answers1

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.

Bill
  • 1,588
  • 12
  • 21
  • 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... – zw骑猪o0o找牛 Nov 25 '18 at 15:49
  • 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. – Bill Nov 26 '18 at 01:37
  • 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? override func touchesBegan(_ touches: Set, 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") } } – zw骑猪o0o找牛 Nov 26 '18 at 09:27
  • 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. – Bill Nov 27 '18 at 22:10