How to change NSString value while debugging in Xcode?

XcodeDebugging

Xcode Problem Overview


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.

Xcode Solutions


Solution 1 - Xcode

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.

Solution 2 - Xcode

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.

Solution 3 - Xcode

For NSError I am using this:

(lldb) expression aTempError = (NSError*)[[NSError alloc] initWithDomain:@"MANO" code:1 userInfo:nil]

Solution 4 - Xcode

(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".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxmartinView Question on Stackoverflow
Solution 1 - XcodeMikeGView Answer on Stackoverflow
Solution 2 - Xcoden8trView Answer on Stackoverflow
Solution 3 - XcodeRamisView Answer on Stackoverflow
Solution 4 - XcodeAlex ZavatoneView Answer on Stackoverflow