在 Xcode 中调试时如何更改 NSString 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4858159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change NSString value while debugging in Xcode?
提问by xmartin
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 的断点处停止时,我可以看到 NSString 变量的值。我怎样才能改变它们?我可以更改 int 或 double 变量,但不能更改 NSString。
回答by MikeG
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.
您可以在调试控制台中执行此操作。说你有NSString* myVar
。在控制台中,在 之后(gdb)
,键入set myVar = @"My new string"
。如果您使用的是(lldb)
,请改用等效的表达式expr myVar = @"My new string"
。
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.
这可能不会在变量面板中正确显示,但您可以通过进入po myVar
控制台来验证该值。您的代码应该选择新值。
For some great info about using expr
, check out this StackOverflow post.
有关使用的一些重要信息expr
,请查看此 StackOverflow 帖子。
回答by n8tr
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:
然后在 (gdb) 提示符下键入:
call myString = @"My new string"
You can po myString
before changing the string and after you change it to verify that it has changed.
您可以po myString
在更改字符串之前和更改字符串之后验证它是否已更改。
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.
继续运行程序,您应该会看到视图控制器的标题更新。
回答by Ramis
For NSError I am using this:
对于 NSError 我正在使用这个:
(lldb) expression aTempError = (NSError*)[[NSError alloc] initWithDomain:@"MANO" code:1 userInfo:nil]
回答by Alex Zavatone
(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".
( https://stackoverflow.com/users/1202867/n8tr) n8tr 上面的评论显示了如何做到这一点。你只需在你的任务之前加上“po”,就像这样:po myString = @"my_new_string" 和 po self.title = @"New Title"。