Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

ios - Why is Xcode's Variables View's "Edit Value" not changing the variable value?

In Xcode's Variables View, on the right of the Debug area, when an app is running and paused at a breakpoint you can right-click a variable and select "Edit Value".

For a swift String it's greyed out, and I can imagine why that might be the case. But even for a simple int, it brings up an edit box to enter an new value, but after hitting the value stays at the old value. This is true even for a var which is changed during the code.

Update: as shown in Jim's answer below, you should be able to set the value using the lldb expression command, but, although Xcode will tell you it has changed, it fails to actually change the value.

Is this broken, or is there something specific you need to do for it to work? Thanks.

Screenshot

Update: It's a compile bug - see Jim's comment. Here's a workaround...

    println("Before lldb change, foo is (foo)")
    //make compiler think foo may change, so I can change it myself at the console
    if count("abcd") == 0 { foo = 0 }
    println("After lldb change, code now thinks foo is (foo)")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most Swift entities, for sure strings but even "simple" types like Int's, are actually not simple types. The values you see in the Variables View are constructed by data formatters in lldb that are set up to present a useful view of the Values without running any code (for performance reasons.) They know how grub around & fetch the contents of Swift types, but we didn't teach them how to edit values, only present them.

If you want to change a value, you need to run some code in your program to do that, which you can do using the expression command in the lldb console. So for instance if you have an Int variable called foo, you can do:

(lldb) expr foo = 12

That will compile & execute that code fragment, and of course the Swift compiler does know how to alter these Swift values, so the resultant code will correctly set the value.

Note, it does appear that the swift compiler will sometimes copy a value to a register and use it from the register w/o indicating that fact in the debug info. If that happens, lldb will report the value it set in the location the debug information pointed to, but the code will actually use the temporary value. I've filed a bug with the swift compiler demonstrating one instance of this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...