Ever since I updated to Xcode 12, I have not been able to print out variables in the console while debugging with lldb.
Using print()
statements directly in Swift code outputs to the console as expected.
A command in the console such as po "hi"
outputs "hi" to the console as expected.
Trying to run po <variable_name>
, instead, always outputs this line:
error: <EXPR>:3:1: error: cannot find 'variable_name' in scope
instead of the expected variable's value.
Add these lines to Swift code
let example = "hi"
print(example)
Put a breakpoint on the second line, the one with the print()
statement
Run the code and wait for the breakpoint to be hit
Try to run po example
in the debug console
It should print "hi"
in the console
It prints this error line
error: <EXPR>:3:1: error: cannot find 'example' in scope
It's the same when running on the simulator or a device. I've tried activating the console, and restarting Xcode.
I've tried creating a new project in Xcode12 and that works fine, my other older projects created with previous versions of Xcode work fine in Xcode 12.
I've tried confronting the Build settings both with the other projects and with a newly created project and it seems nothing's wrong: optimization is set to None
as it should for debug build configurations and the run scheme is set to launch a debug build configuration.
Where else could the problem be?
1. The po command is added to the built-in command set in lldb using: "command alias po expr -O --". If you run help po the last line in the output is: "'po' is an abbreviation for 'expression -O --'. The -- indicates the end of the command options, so all your input on the line is your expression.
The po command uses the object description, whereas the p and v commands use the data formatters to display the object on the console. A second important difference is that both po and p compile expressions and then execute them and have access to the full language.
See variable values in code and the variable viewer When your app pauses at a breakpoint, hover over a variable in your source code to view its current value. If the variable is an image or other type that isn't expressible as text, click the Quick Look button at the upper-right to see a preview of the variable.
In Swift, you can print a variable or a constant to the screen using the print() function. Let's see that in action by creating a playground file, as you learned in the last lesson. After creating your new playground in Xcode, start by declaring three strings representing your favorite word, color, and musician.
You can change optimization level to "none" value in build settings. Its works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With