objective-c 如何使用 NSLog(@"Inside of the iPhone Simulator") 进行调试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/558568/
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 do I debug with NSLog(@"Inside of the iPhone Simulator")?
提问by Rob Sawyer
I'm used to programming and having log messages be viewable. I know you used to be able to use NSLog()to trace out messages when debugging Cocoa applications. What is the best way to "trace" messages when coding in an iPhone Xcode development environment?
我习惯于编程和查看日志消息。我知道您曾经能够NSLog()在调试 Cocoa 应用程序时用来跟踪消息。在 iPhone Xcode 开发环境中编码时“跟踪”消息的最佳方法是什么?
回答by cdespinosa
There's a far more convenient way to trace with log messages in Xcode, and that's using Breakpoint Actions.
在 Xcode 中有一种更方便的跟踪日志消息的方法,那就是使用断点操作。
On the line of code where you'd be tempted to add a printf or NSLog, set a breakpoint, then control-click it and choose "Edit Breakpoint". In the blue bubble that appears, click the + button on the right to open the Breakpoint Actions: alt text http://idisk.mac.com/cdespinosa/Public/Breakpoint%20Actions.png
在您想要添加 printf 或 NSLog 的代码行上,设置一个断点,然后按住 Control 键单击它并选择“编辑断点”。在出现的蓝色气泡中,单击右侧的 + 按钮打开 Breakpoint Actions: 替代文本 http://idisk.mac.com/cdespinosa/Public/Breakpoint%20Actions.png
Enter your log text there. Any expression that can be printed in the Debugger can be used when delimited by @ signs.
在那里输入您的日志文本。当用@ 符号分隔时,可以使用任何可以在调试器中打印的表达式。
For debugging Objective-C it's generally more useful to choose "Debugger Command" from the popup and enter 'po [[object method] method]' to print the description string of an Objective-C object or the result of a method call.
对于调试 Objective-C,通常更有用的是从弹出窗口中选择“调试器命令”并输入“po [[object method] method]”以打印 Objective-C 对象的描述字符串或方法调用的结果。
Make sure to click the "Continue" checkbox at the top right so execution continues after the log.
确保单击右上角的“继续”复选框,以便在日志后继续执行。
Advantages of this over NSLog and printf:
相对于 NSLog 和 printf 的优点:
- It's on the fly. You don't have to recompile and restart to add or edit log messages. This saves you a lot of time.
- You can selectively enable and disable them. If you learn enough from one, but its spew is interfering, just uncheck its Enabled box.
- All the output is generated on your Mac, never on the iPhone, so you don't have to download and parse through logs after the fact.
- The chance of shipping console spew in your application is significantly decreased.
- 它在飞行中。您不必重新编译并重新启动即可添加或编辑日志消息。这可以为您节省大量时间。
- 您可以有选择地启用和禁用它们。如果您从其中学到了足够的知识,但它的喷出干扰了,只需取消选中其启用框即可。
- 所有输出都在您的 Mac 上生成,而不是在 iPhone 上生成,因此您不必事后下载和解析日志。
- 在您的应用程序中发送控制台溢出的机会显着降低。
Also check out the Speak button; it's great for debugging full-screen apps where you can't see the debug log.
还要检查“说话”按钮;它非常适合调试无法看到调试日志的全屏应用程序。
回答by Elliot
Here's a great bit of code I picked up somewhere on the web. It defines new functions DLog() and ALog(). DLog messages only appear when the app is compiled with a -DDEBUG flag (define DEBUG). ALog messages ALWAYS appear (even in Release mode).
这是我在网上某处找到的大量代码。它定义了新函数 DLog() 和 ALog()。DLog 消息仅在使用 -DDEBUG 标志(定义 DEBUG)编译应用程序时出现。ALog 消息总是出现(即使在发布模式下)。
// DLog is almost a drop-in replacement for NSLog
// DLog();
// DLog(@"here");
// DLog(@"value: %d", x);
// Unfortunately this doesn't work DLog(aStringVariable); you have to do this instead DLog(@"%@", aStringVariable);
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
回答by Jane Sales
In my project I have a customised solution based on DebugOutput.mThis adds the file & line number to the debug output, making it easier to identify where that output text is coming from, while still keeping it brief.
在我的项目中,我有一个基于DebugOutput.m的自定义解决方案,这将文件和行号添加到调试输出中,从而更容易识别输出文本的来源,同时仍然保持简短。
I've augmented the standard solution with a debug mask, so that I can switch debugging on and off for particular areas of functionality in my app. In Debug.h, I have
我使用调试掩码增强了标准解决方案,以便我可以打开和关闭应用程序中特定功能区域的调试。在 Debug.h 中,我有
typedef enum {
kDebugMaskAp- = 1,
kDebugMaskXMLParser = 1 << 1,
kDebugMaskNetwork = 1 << 2,
kDebugMaskAnalytics = 1 << 3,
kDebugMaskCache = 1 << 4,
} debugBitMask;
#define debugForComponent(mask,format,...) if( currentDebugMask() & mask) [[DebugOutput sharedDebug] output:__FILE__ lineNumber:__LINE__ input:(format), ##__VA_ARGS__]
And in Debug.m
并在 Debug.m
-(void)output:(char*)fileName lineNumber:(int)lineNumber input:(NSString*)input, ...
{
va_list argList;
NSString *filePath, *formatStr;
// Build the path string
filePath = [[NSString alloc] initWithBytes:fileName length:strlen(fileName) encoding:NSUTF8StringEncoding];
// Process arguments, resulting in a format string
va_start(argList, input);
formatStr = [[NSString alloc] initWithFormat:input arguments:argList];
va_end(argList);
// Call NSLog, prepending the filename and line number
NSLog(@"File:%s Line:%d %@",[((DEBUG_SHOW_FULLPATH) ? filePath : [filePath lastPathComponent]) UTF8String], lineNumber, formatStr);
[filePath release];
[formatStr release];
}
In the application, calls look something like this:
在应用程序中,调用如下所示:
debugForComponent(kDebugMaskApp,@"Request failed - error %@", [error localizedDescription]);
回答by Abhishek Bedi
Paste this in your prefix header. ALL logs from project will dissappear for sure .
将此粘贴到您的前缀标题中。项目中的所有日志肯定会消失。
#ifndef __OPTIMIZE__
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...) {}
#endif
回答by fpillet
You could use NSLoggerwhich brings a lot more to the table than just logging your messages. I use macros to disable logs in release builds, while leaving every single one of them active in debug builds. The log volume is not an issue, as NSLogger offers powerful log filtering options.
您可以使用NSLogger来为表格带来更多内容,而不仅仅是记录您的消息。我使用宏在发布版本中禁用日志,同时在调试版本中让每个日志都处于活动状态。日志量不是问题,因为 NSLogger 提供了强大的日志过滤选项。
回答by fpillet
I simply use the replace all functionality....
我只是使用替换所有功能....
I disable all my NSLog statements by replacing NSLog(@" with //***NSLog(@"
我通过将 NSLog(@" 替换为 //***NSLog(@"
That way I can simply find it (using find in all project files) with //***NSLog(@" and re-enable them
这样我就可以简单地找到它(在所有项目文件中使用 find) //***NSLog(@" 并重新启用它们
Nothing fancy but it works :)
没什么特别的,但它有效:)

