您的位置:首页 > 移动开发 > IOS开发

iOS开发:NSLog使用技巧

2015-01-22 16:30 155 查看


前提:在XCode做开发调试时往往需要打印一些调试信息做debug用,大家知道当打印信息的地方多了之后在模拟器上跑可能不会有什么问题,因为模拟器用的是电脑的硬件但是当应用跑在设备上时这些输出语句会在很大程度上影响应用的性能,针对这种问题可以写一些宏来控制这些调试信息的输出。


在release版本禁止输出NSLog内容

因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉。

我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时,再取消这些注释,这实在是一件无趣而耗时的事!还好,还有更优雅的解决方法,就是在项目的prefix.pch文件里加入下面一段代码,加入后,NSLog就只在Debug下有输出,Release下不输出了。


如何实现:

在-Prefix.pch(pch全称是“precompiled header”,也就是预编译头文件,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器编译速度。我们知道当我们修改一个工程中某个文件代码时候,编译器并不是重新编译所有所有文件,而是编译改动过文件的,假如pch中某个文件修改了,那么pch整个文件里包含的的其他文件也会重新编译一次,这样就会消耗大量时间,所以它里面添加的文件最好是是很少变动或不变动的头文件或者是预编译的代码片段;)文件中添加

[plain] view
plaincopy

#ifdef DEBUG

#define NSLog(fmt,...) do{NSLog(fmt,##__VA_ARGS__);} while(0)

#define debugMethod() NSLog(@"%s", __func__)

#else

#define NSLog(...) do{}while(0)

#define debugMethod()

#endif

其他的各种例子:

<a target=_blank target="_blank" id="L1" href="https://code.csdn.net/dashboard/snippets#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank target="_blank" id="L2" href="https://code.csdn.net/dashboard/snippets#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank target="_blank" id="L3" href="https://code.csdn.net/dashboard/snippets#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank target="_blank" id="L4" href="https://code.csdn.net/dashboard/snippets#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank target="_blank" id="L5" href="https://code.csdn.net/dashboard/snippets#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank target="_blank" id="L6" href="https://code.csdn.net/dashboard/snippets#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank target="_blank" id="L7" href="https://code.csdn.net/dashboard/snippets#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank target="_blank" id="L8" href="https://code.csdn.net/dashboard/snippets#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank target="_blank" id="L9" href="https://code.csdn.net/dashboard/snippets#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank target="_blank" id="L10" href="https://code.csdn.net/dashboard/snippets#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank target="_blank" id="L11" href="https://code.csdn.net/dashboard/snippets#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank target="_blank" id="L12" href="https://code.csdn.net/dashboard/snippets#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank target="_blank" id="L13" href="https://code.csdn.net/dashboard/snippets#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank target="_blank" id="L14" href="https://code.csdn.net/dashboard/snippets#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank target="_blank" id="L15" href="https://code.csdn.net/dashboard/snippets#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank target="_blank" id="L16" href="https://code.csdn.net/dashboard/snippets#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
//DLog will output like NSLog only when the DEBUG variable is set#ifdef DEBUG    #define DLog(fmt, ...) do{ NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)#else    #define DLog(...) do{}while(0)#endif
// ALog will always output like NSLog#define ALog(fmt, ...) do{NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)
// ULog will show the UIAlertView only when the DEBUG variable is set#ifdef DEBUG    #define ULog(fmt, ...)  do{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }while(0)#else    #define ULog(...) do{}while(0)#endif


上段代码的意思就是 用宏指令做一个判断,如果DEBUG为真,则编译#ifdef到#endif宏定义,否则编译器就不编译;

这个DEBUG在哪设置呢,

在 "Target > Build Settings > Preprocessor Macros > Debug" 里有一个"DEBUG=1"。
设置为Debug模式下,Product-->Scheme-->SchemeEdit Scheme

设置Build Configuration成Debug时,就可以打印NSLog了。

设置Release,发布app版本的时候就不会打印了,提高了性能

附加知识:

文中提到的几个宏这里再简单解释一下:

[1]__VA_ARGS__是一个可变参数的宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有GCC支持(VC6.0编译器不支持)。宏前面加上##的作用在于,当可变参数的个数为0时,这里的##起到把前面多余的","去掉,否则会编译出错。
[2]__FILE__宏在预编译时会替换成当前的源文件名
[3]__LINE__宏在预编译时会替换成当前的行号
[4]__FUNCTION__宏在预编译时会替换成当前的函数名称

另外也可以使用下面的方法来重写系统的NSLog

#ifndef __OPTIMIZE__//__OPTIMIZE__是release默认会加的宏

#define NSLog(fmt,...) do{NSLog(fmt,##_VA_ARGS__);}while(0)

#else

#define NSLog(fmt,...) do{}while(0)

#endif

或者写成:

#ifndef DEBUG//这里必须在 "Target > Build Settings > Preprocessor Macros > Debug" 里设置"DEBUG=1"。(因为有的情况下,这个设置为空,需手动设置。如Unity3D生成的Xcode代码默认就没有设置)

#ifdef DEBUG
#define NSLog(format, ...) do{ \
fprintf(stderr,"<%s : %d> %s\n", \
[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \
__LINE__, __func__); \
(NSLog)((format), ##__VA_ARGS__); \
fprintf(stderr,"-------\n"); \
} while (0)
#else
#define NSLog(format, ...) do{}while(0)//release模式下,忽略
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: