您使用过的NSLog有哪些独特的用途?

Amb*_*tar 0 cocoa objective-c nslog

您是否有任何用于调试的NSLog的独特或特殊用途?

Mar*_*eau 11

我喜欢用这种格式进行调试.

NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
Run Code Online (Sandbox Code Playgroud)

当然,您需要将它包装在您自己的方法或函数中以方便使用.我使用预处理器,并且只为我自己使用它以及我发送给beta测试者的特殊构建.顺便提一句,这是从我对这个问题的回答中复制而来的.

#define DEBUG_MODE

#ifdef DEBUG_MODE
    #define DebugLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
    #define DebugLog( s, ... ) 
#endif
Run Code Online (Sandbox Code Playgroud)

这样做DebugLog就像NSLog,但显示调用它的文件名和行号:

2009-05-23 17:23:40.920 myproject[92523:10b] <AppCon.m:(8)> My debug message...
Run Code Online (Sandbox Code Playgroud)