- Objective-C基础
- Objective-C - 首页
- Objective-C - 概述
- Objective-C - 环境搭建
- Objective-C - 程序结构
- Objective-C - 基本语法
- Objective-C - 数据类型
- Objective-C - 变量
- Objective-C - 常量
- Objective-C - 运算符
- Objective-C - 循环
- Objective-C - 条件判断
- Objective-C - 函数
- Objective-C - 块 (Blocks)
- Objective-C - 数字
- Objective-C - 数组
- Objective-C - 指针
- Objective-C - 字符串
- Objective-C - 结构体
- Objective-C - 预处理器
- Objective-C - Typedef
- Objective-C - 类型转换
- Objective-C - 日志处理
- Objective-C - 错误处理
- 命令行参数
- 高级Objective-C
- Objective-C - 类与对象
- Objective-C - 继承
- Objective-C - 多态
- Objective-C - 数据封装
- Objective-C - 分类 (Categories)
- Objective-C - 模拟 (Posing)
- Objective-C - 扩展 (Extensions)
- Objective-C - 协议 (Protocols)
- Objective-C - 动态绑定
- Objective-C - 组合对象
- Obj-C - Foundation框架
- Objective-C - 快速枚举
- Obj-C - 内存管理
- Objective-C有用资源
- Objective-C - 快速指南
- Objective-C -有用资源
- Objective-C - 讨论
Objective-C日志处理
NSLog方法
为了打印日志,我们在Objective-C编程语言中使用NSLog方法,从HelloWorld示例开始我们就一直在使用它。
让我们看一段打印“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!
在正式应用中禁用日志
由于我们在应用程序中使用的NSLog会打印在设备的日志中,在正式版本中打印日志并不理想。因此,我们使用类型定义来打印日志,我们可以像下面这样使用它们。
#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
广告