How to change variables value while debugging with LLDB in Xcode?
XcodeDebuggingLldbXcode Problem Overview
In Xcode, GDB allows you to change local variables while debugging (see https://stackoverflow.com/questions/4858159/how-to-change-nsstring-value-while-debugging-in-xcode). Does LLDB offer a similar functionality? If so, how can we use it?
Xcode Solutions
Solution 1 - Xcode
expr myString = @"Foo"
> (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
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'
Solution 2 - Xcode
The following stuff works for me. I am using Xcode 8.
If you want to set some variable (for example a "dict") to nil and then test the code flow, you can try the following.
- Put the breakpoint properly after initialised to the desired value.
- then execute "expression dict = nil" in lldb command line to change it. (for example "nil")
- Step over the break point.
- Check the variable "dict" in the next line. It will be nil.
It will look something like in the console.
(lldb) expression dict = nil
(NSDictionary *) $5 = nil
Solution 3 - Xcode
If you are using Xcode 10 or 11 put the breakpoint properly after initialised to the required variable then you can change your variable using po myString = "Hello World"
easily.