发布于 2015-07-05 11:41:40 | 241 次阅读 | 评论: 0 | 来源: 网络整理

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

现在,当我们编译和运行的程序在释放模式,我们会得到以下的结果。


2013-09-11 02:47:45.248 demo[3158] NSLog gets printed always
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务