expr stringToBeChangedWhileDebugging = @"New Changed String
"
Problem:
How to change the values of variables while you are debugging with LLVM in XCode.For Example, you put a breakpoint in your code and while running it, you reach that breakpoint. So now you must have used the 'p' and 'po' commands in LLVM of XCode which help a lot while debugging by printing the values of passed variables.
But in this article, we will tell you how to change those values:
Solution:
We can use expr command provided. It can be used as:
What is 'expr' command?
(lldb) help expr
Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope. This command takes 'raw' input (no need to quote stuff).Syntax: expression --Command Options Usage: expression [-f ] [-G ] [-d ] [-u ] -- expression [-o] [-d ] [-u ] -- expression-G <gdb-format> ( --gdb-format <gdb-format> )
Specify a format using a GDB format specifier string.
-d <boolean> ( --dynamic-value <boolean> )
Upcast the value resulting from the expression to its dynamic type
if available.
-f <format> ( --format <format> )
Specify a format to be used for display.
-o ( --object-description )
Print the object description of the value resulting from the
expression.
-u <boolean> ( --unwind-on-error <boolean> )
Clean up program state if the expression causes a crash, breakpoint
hit or signal.Examples:expr my_struct->a = my_array[3]
expr -f bin -- (index * 8) + 5
expr char c[] = "foo"; c[0]IMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options you must use ' -- ' between the end of the command options and the beginning of the raw input.'expr' is an abbreviation for 'expression'
If you try to change the text of a UILabel:
'expr myLabel.text = @"I can't be changed like this :("
then you will get the following error:
error: property 'text' not found on object of type 'UILabel *'
Actually, lldb handles dot syntax much better than gdb. gdb just assumes you're treating it like a C-struct, which fails. lldb will correctly access properties, but only if they're actually declared with
@property
So to change to change the text of a UILabel, you can use:
expr (void)[myLabel setText:@"I can be changed now :)"]
Dot-Syntax doesn't work for
po
either. instead of po
myLabel.text
you have to use:po [myLabel text]
Using 'p' to change values while debugging:
You can also use
p
as a shortcut for expr
. Example:
(lldb) p stringToBeChangedWhileDebugging = @"New Changed String"