如何从 Xcode 4 中的断点操作打印字符串值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/742456/
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 print string value from breakpoint action in Xcode 4?
提问by 4thSpace
I have a breakpoint action and am using the Log option from the drop down. I'd like to print out the string (summary) value. I'm doing this:
我有一个断点操作,正在使用下拉列表中的日志选项。我想打印出字符串(摘要)值。我这样做:
the person name is: @p.name@
but that prints the memory address. I can switch to the Debugger Command option and do
但这会打印内存地址。我可以切换到调试器命令选项并执行
po f.name
but then I lose my description, as used in the first option. Using the Log option, is there a way to print the string value and not the memory address?
但随后我丢失了我在第一个选项中使用的描述。使用 Log 选项,有没有办法打印字符串值而不是内存地址?
采纳答案by Jason Coco
There are a couple of things you can do. Add a Log function from the drop-down, then add another box and select Debugger Command
and enter po f.name
.
您可以做几件事。从下拉列表中添加一个 Log 函数,然后添加另一个框并选择Debugger Command
并输入po f.name
。
If you want your log to be more complicated, you could do something more like this:
如果你想让你的日志更复杂,你可以做更多这样的事情:
the person name is: @(const char *)[(NSString*)[p.name description] UTF8String]@
You best bet is probably to just use the debugger's graphical interface to watch variables. If you want to log messages, use NSLog
.
您最好的选择可能是仅使用调试器的图形界面来观察变量。如果要记录消息,请使用NSLog
.
回答by auco
You can use NSLog statements with breakpoints using "Debugger Command", but you need to add "expr"
您可以使用带有断点的 NSLog 语句使用“调试器命令”,但您需要添加“expr”
expr (void)NSLog(@"The Person Name is %@", p.name)
-
——
回答by Skotch
Here is a solution using only one action, and using fewer characters than the expr
solution.
这是一个仅使用一个操作的解决方案,并且使用的字符比expr
解决方案少。
However, unless you add the void
like this:
但是,除非你添加void
这样的:
po (void)NSLog(@"the person name is: %@", p.name)
you will get an annoying "nil" printed out with your log. for example:
您会在日志中打印出令人讨厌的“nil”。例如:
(lldb) po NSLog(@"foo")
nil
2013-06-19 14:42:59.025 TheMove[95864:c07] foo
(lldb) po (void)NSLog(@"foo")
2013-06-19 14:43:10.758 TheMove[95864:c07] foo
If you can live with the nil (I can) it's faster to type and easier to remember just the po
如果您可以接受 nil(我可以),那么键入会更快,并且更容易记住 po
回答by cescofry
I ended up using 2 different actions for the same breakpoint. First a Log and then a debugger command with po varName. Only downside it will print in 2 different rows.
我最终对同一个断点使用了 2 个不同的操作。首先是日志,然后是带有 po varName 的调试器命令。唯一的缺点是它会打印成 2 行。
回答by LukeSideWalker
You don't need TWO actionsin the breakpoint, you can just have ONEcommand (log message), where you put the message including the content of variables (see image). By clicking "automatically continues" it is just acting like having a print-command in code, but the good thing is, then you dont have print statements in your code.
您不需要在断点中执行两个操作,您可以只有一个命令(日志消息),您可以在其中放置包括变量内容的消息(见图)。通过单击“自动继续”,它就像在代码中包含打印命令一样,但好处是,您的代码中没有打印语句。