请帮助我,我是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")
}
}
我想使用lldb修改'flag'的值,所以
(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
它似乎没有用。 请告诉我如何使用lldb,修改bool值。
答案 0 :(得分:0)
首先,这似乎是Swift和LLDB之间的问题。我怀疑Swift正在将var标志优化成一个寄存器。关于类似问题还有其他一些SO问题,例如:Why is Xcode's Variables View's “Edit Value” not changing the variable value?。有趣的是,Xcode可以解决此问题。解决方法似乎是“欺骗” LLDB以识别变量已更新。我修改了您的代码,如下所示:
//
// 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")
}
以下是我的LLDB命令简化(输出缩进):
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
也许其他人可以提供更多信息或深入了解Xcode如何解决此问题。