55

When I'm stopped at a break point in Xcode, I can see the value of NSString variables. How can I change them? I can change int or double variables, but not NSString.

shim
  • 9,289
  • 12
  • 69
  • 108
xmartin
  • 585
  • 1
  • 4
  • 5
  • in 1987 we could easily use the equivalent of Xcode's "Edit Value..." in all the *real* debuggers. Now we are relegated to writing debugger code... Back to the Future... – Cerniuk Jan 05 '18 at 14:22

4 Answers4

88

You can do this in the debug console. Say you have NSString* myVar. In the console, after (gdb), type set myVar = @"My new string". If you are using (lldb), then use the equivalent expression expr myVar = @"My new string" instead.

This may not show up correctly in the variables panel, but you can verify the value by entering po myVar into the console. Your code should pick up the new value.

For some great info about using expr, check out this StackOverflow post.

Community
  • 1
  • 1
MikeG
  • 4,015
  • 2
  • 27
  • 25
  • This doesn't work for me in XCode 4.3.1 using (lldb). I get errors about "reference to 'id' is ambiguous" and "1 errors parsing expression". – funroll Jul 12 '12 at 13:23
  • 2
    You are hitting a variation of a known bug in XCode 4.3.x. See [this post](http://stackoverflow.com/questions/9515630/lldb-fails-to-print-variable-values-with-error-reference-to-id-is-ambiguous). According to the answer, this is resolved in XCode 4.4 – MikeG Aug 09 '12 at 17:21
  • Using expr whateverVariable = @"55fc633a22570e2b7e00003b" worked for me. – finneycanhelp Oct 09 '15 at 16:28
  • Its about the same amount of work to put: NSString * whateverVariable = @"55fc633a22570e2b7e00003b"; in the code itself. Sigh – Cerniuk Jan 05 '18 at 14:29
8

You can but you have to call code from the debugger command prompt. For example, say you have a breakpoint fire off right after this line:

NSString *myString = @"My current string";

Then at the (gdb) prompt type:

call myString = @"My new string"

You can po myString before changing the string and after you change it to verify that it has changed.

Another example: Say you wanted to change a view controller's title. You can use the setter. *Note: dot notation is not supported at the debugger command line. For example let the view load and then set a breakpoint somewhere during the lifetime of your view controller. Then do this:

call (id)[self setTitle:@"New Title"]

Continue running the program and you should see your view controller's title update.

n8tr
  • 5,018
  • 2
  • 32
  • 33
  • 5
    my answer is old. You can actually do this now: **po myString = @"my_new_string"** and **po self.title = @"New Title"** – n8tr Jul 03 '14 at 00:54
2

For NSError I am using this:

(lldb) expression aTempError = (NSError*)[[NSError alloc] initWithDomain:@"MANO" code:1 userInfo:nil]
Ramis
  • 13,985
  • 7
  • 81
  • 100
1

(https://stackoverflow.com/users/1202867/n8tr) n8tr's comment from above shows how to do it. You just precede your assignment with "po " like so: po myString = @"my_new_string" and po self.title = @"New Title".

Community
  • 1
  • 1
Alex Zavatone
  • 4,106
  • 36
  • 54