为什么 Xcode 的变量视图的“编辑值”没有改变变量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31367005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is Xcode's Variables View's "Edit Value" not changing the variable value?
提问by DenverCoder9
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".
在 Xcode 的变量视图中,在调试区域的右侧,当应用程序正在运行并在断点处暂停时,您可以右键单击变量并选择“编辑值”。
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.
对于 swift String 它是灰色的,我可以想象为什么会这样。但即使是一个简单的 int,它也会弹出一个编辑框来输入一个新值,但点击该值后仍为旧值。即使对于在代码期间更改的 var 也是如此。
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.
更新:如下面 Jim 的回答所示,您应该能够使用 lldb 表达式命令设置该值,但是,尽管 Xcode 会告诉您它已更改,但它实际上并没有更改该值。
Is this broken, or is there something specific you need to do for it to work? Thanks.
这是否已损坏,或者您需要做些什么才能使其正常工作?谢谢。
Update: It's a compile bug - see Jim's comment. Here's a workaround...
更新:这是一个编译错误 - 请参阅Jim 的评论。这是一个解决方法...
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)")
回答by Jim Ingham
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.
大多数 Swift 实体,当然是字符串,但即使是像 Int 这样的“简单”类型,实际上都不是简单类型。您在变量视图中看到的值是由 lldb 中的数据格式化程序构造的,这些格式化程序设置为在不运行任何代码的情况下呈现有用的值视图(出于性能原因)。他们知道如何 grub around & fetch Swift 类型的内容,但我们没有教他们如何编辑值,只是展示了它们。
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:
如果你想改变一个值,你需要在你的程序中运行一些代码来做到这一点,你可以使用expression
lldb 控制台中的命令来完成。因此,例如,如果您有一个名为 的 Int 变量foo
,您可以执行以下操作:
(lldb) expr foo = 12
(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.
这将编译并执行该代码片段,当然 Swift 编译器确实知道如何更改这些 Swift 值,因此生成的代码将正确设置该值。
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.
请注意,swift 编译器似乎有时会将值复制到寄存器并从寄存器中使用它,而无需在调试信息中指示该事实。如果发生这种情况,lldb 将报告它在调试信息指向的位置设置的值,但代码实际上将使用临时值。我已经向 swift 编译器提交了一个错误,展示了一个例子。
回答by user1232690
In case of constant Integers, you can't directly set anything.
在常量整数的情况下,您不能直接设置任何内容。
However let's say you have variable passed as a parameter to your function:
但是,假设您已将变量作为参数传递给您的函数:
value = (AnyObject!) Int64(38)
instance_type = (Builtin.RawPointer) 0xb000000000000263
value = (AnyObject!) Int64(38)
instance_type = (Builtin.RawPointer) 0xb000000000000263
Note that this address starting with 1 is not a real pointer. Now let's say you want to replace value 38 with 64. You type:
请注意,这个以 1 开头的地址不是真正的指针。现在假设您想用 64 替换值 38。您键入:
po Unmanaged.passUnretained(64).toOpaque()
and you've got magical pseudo address of constant 64:
po Unmanaged.passUnretained(64).toOpaque()
你得到了常量 64 的神奇伪地址:
0xb000000000000403
- _rawValue : (Opaque Value)
0xb000000000000403
- _rawValue : (Opaque Value)
then you replace 0xb000000000000263 with 0xb000000000000403 and you've got your constant changed.
然后你用 0xb000000000000403 替换 0xb000000000000263 并且你已经改变了你的常数。
God, I love Swift
天啊,我爱斯威夫特