如何在 xcode 中创建断点的日志消息操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8059919/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:22:18  来源:igfitidea点击:

how to create a breakpoint's log message action in xcode?

xcodeactionbreakpointsnslog

提问by PrimeSeventyThree

Been watching a WWDC video today about new features in xCode 4. They have mentioned that it a good idea to use log message actions on breakpoints along with "automatically continue after evaluation actions" enabled to output a variable's value for instance instead of using NSLogs all the time.

今天一直在观看有关 xCode 4 中新功能的 WWDC 视频。他们提到在断点上使用日志消息操作以及启用“评估操作后自动继续”以输出变量的值是个好主意,而不是使用 NSLogs all时间。

lets say I have something like that:

可以说我有这样的事情:

NSLog(@"URL is : %@", userDocumentsURL);

How would I write a log message action to display userDocumentsURL's value? Is it really a good idea to use the above method instead of NSLog?

我将如何编写日志消息操作来显示 userDocumentsURL 的值?用上面的方法代替NSLog真的是个好主意吗?

回答by Josh

Create a Breakpoint 'Log Message' action. For the log message include something like:

创建断点“日志消息”操作。对于日志消息,包括以下内容:

URL is @(char*) [[userDocumentsURL description] UTF8String]@

Alternatively you can create a breakpoint 'Debugger command' action similar to:

或者,您可以创建类似于以下内容的断点“调试器命令”操作:

po [NSString stringWithFormat:@"URL is: %@", userDocumentsURL]

I prefer using breakpoint actions for logging, as it's arguably easier to clear out a bunch of breakpoints than it is to remove NSLogs. A possible downside to using breakpoints in this fashion is that they are significantly slower (during debugging) than a direct NSLog.

我更喜欢使用断点操作进行日志记录,因为可以说清除一堆断点比删除 NSLogs 更容易。以这种方式使用断点的一个可能的缺点是它们比直接 NSLog 慢得多(在调试期间)。

回答by Skotch

Here is a similar solution using NSLog, which might be fewer characters than the other solutions.

这是使用 的类似解决方案NSLog,它可能比其他解决方案的字符少。

debugger command using NSlog

使用 NSlog 的调试器命令

However, unless you add the voidlike 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 Barton Friedland

I notice that the edit breakpoints feature, while useful and perhaps modern, does not commit to source control, so does not scale to a team of developers. For this reason, I would say that when working on a team under source control to stick with code-based logging such as NSLog.

我注意到编辑断点功能虽然有用且可能很现代,但不致力于源代码控制,因此无法扩展到开发团队。出于这个原因,我会说在源代码控制下的团队工作时,坚持使用基于代码的日志记录,例如 NSLog。