Objective-C 日志处理
NSLog 方法
为了打印日志,我们使用了 Objective-C 编程语言中的 NSLog 方法,我们在 Hello World 示例中使用了该方法。
让我们看一个打印"Hello World"字样的简单代码 −
#import <Foundation/Foundation.h>
int main() {
NSLog(@"Hello, World! \n");
return 0;
}
现在,当我们编译并运行程序时,我们将得到如下结果。
2013-09-16 00:32:50.888 demo[16669] Hello, World!
在 Live 应用中禁用日志
由于我们在应用程序中使用了 NSLogs,因此它将打印在设备日志中,并且在实时构建中打印日志并不好。 因此,我们使用类型定义来打印日志,我们可以如下所示使用它们。
#import <Foundation/Foundation.h>
#if DEBUG == 0
#define DebugLog(...)
#elif DEBUG == 1
#define DebugLog(...) NSLog(__VA_ARGS__)
#endif
int main() {
DebugLog(@"Debug log, our custom addition gets \
printed during debug only" );
NSLog(@"NSLog gets printed always" );
return 0;
}
现在,当我们在调试模式下编译和运行程序时,我们将得到如下结果。
2013-09-11 02:47:07.723 demo[618] Debug log, our custom addition gets printed during debug only 2013-09-11 02:47:07.723 demo[618] NSLog gets printed always
现在,当我们在release模式下编译运行程序时,会得到如下结果。
2013-09-11 02:47:45.248 demo[3158] NSLog gets printed always

