如何使用NSLog进行debugging(@“iPhone模拟器内部”)?

我习惯于编程,并可以看到日志消息。 我知道你以前可以在debuggingCocoa应用程序时使用NSLog()来查找消息。 在iPhone Xcode开发环境中进行编码时,“跟踪”消息的最佳方式是什么?

在Xcode中跟踪日志消息有一个更方便的方法,那就是使用Breakpoint Actions。

在试图添加printf或NSLog的代码行上,设置一个断点,然后按住Control键点击它,然后select“编辑断点”。 在出现的蓝色气泡中,单击右侧的+button打开断点操作: alt text http://idisk.mac.com/cdespinosa/Public/Breakpoint%20Actions.png

在那里input你的日志文本。 可以在debugging器中打印的任何expression式可以在由@符号分隔时使用。

对于Objective-C的debugging,从popup窗口中select“Debugger Command”并input“po [[object method] method]”来打印Objective-C对象的描述string或方法调用的结果通常会更有用。

确保单击右上angular的“继续”checkbox,以便在日志之后继续执行。

这NSLog和printf的优点:

  • 它在飞行中。 您不必重新编译并重新启动即可添加或编辑日志消息。 这为您节省了很多时间。
  • 您可以select性地启用和禁用它们。 如果你从一个人那里学到了足够多的东西,但是它的效果是干扰的,只需取消选中它的启用框即可。
  • 所有的输出都是在你的Mac上生成的,而不是在iPhone上生成的,所以你不必通过日志来下载和parsing。
  • 在您的应用程序中运输控制台的机会大大减less。

另外检查说话button; debugging无法看到debugging日志的全屏应用程序是非常好的。

这是我在网上某个地方find的很多代码。 它定义了新的函数DLog()和ALog()。 只有使用-DDEBUG标志(定义DEBUG)编译应用程序时才会显示DLog消息。 始终显示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__); 

在我的项目中,我有一个基于DebugOutput.m的自定义解决scheme, 它将文件和行号添加到debugging输出中,使得更容易识别输出文本的来源,同时保持简短。

我使用debugging掩码增强了标准解决scheme,这样我就可以在我的应用程序中为特定的function区域打开和closuresdebugging。 在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__] 

并在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]; } 

在应用程序中,调用如下所示:

 debugForComponent(kDebugMaskApp,@"Request failed - error %@", [error localizedDescription]); 

将其粘贴到您的前缀标题中。 项目中的所有日志都会消失。

 #ifndef __OPTIMIZE__ # define NSLog(...) NSLog(__VA_ARGS__) #else # define NSLog(...) {} #endif 

你可以使用NSLogger ,这比只logging你的消息带来更多的表。 我使用macros来禁用发布版本中的日志,同时将其中的每一个都保存在debugging版本中。 日志量不是问题,因为NSLogger提供了强大的日志过滤选项。

我只是使用replace所有function….

我禁用所有我的NSLog语句通过replaceNSLog(@“与/ / NSLog(@”

这样,我可以简单地find它(使用查找所有项目文件)与/ / NSLog(@“和重新启用它们

没有什么幻想,但它的作品:)