How to view value of Swift “let” constant in Xcode

2019-02-09 21:56发布

问题:

When I'm stopped in the debugger in Xcode 6, how can I view the value of a local Swift constant declared with let?

If I create a brand new Swift project in Xcode 6 and add the following two lines to application(_:didFinishLaunchingWithOptions:) in the app delegate:

let someConstant = 5
var someVariable = 6

…then run the app and break immediately after these lines, this is what I see in the variables view of the debugger:

Why does the variable display its value, while the constant does not? (And why is the constant listed twice?)

If, in the LLDB console, I try p, po, or fr v on someConstant (all of which correctly display the value of someVariable), I get the following:

I'm aware that I can print the value in the debugger by using println in my source code, but I'd really rather not have to have the foresight to do that every time I simply want to inspect a value I've declared as a constant. (Even running expr println(someConstant) in the LLDB console produced the same "unresolved identifier" error as p and po.)

This should be easy. What am I missing?

回答1:

This was a bug in Xcode which I can confirm was fixed in Xcode 6.1. (Thanks, Steve Rosenberg.)

This is what I get now, as expected:

The constant is now displayed correctly in the variables view as well, and is no longer listed twice: