前提:在XCode做开发调试时往往需要打印一些调试信息做debug用,大家知道当打印信息的地方多了之后在模拟器上跑可能不会有什么问题,因为模拟器用的是电脑的硬件但是当应用跑在设备上时这些输出语句会在很大程度上影响应用的性能,针对这种问题可以写一些宏来控制这些调试信息的输出。
在release版本禁止输出NSLog内容
因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉。
我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时,再取消这些注释,这实在是一件无趣而耗时的事!还好,还有更优雅的解决方法,就是在项目的prefix.pch文件里加入下面一段代码,加入后,NSLog就只在Debug下有输出,Release下不输出了。
如何实现:
- #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> |
上段代码的意思就是 用宏指令做一个判断,如果DEBUG为真,则编译#ifdef到#endif宏定义,否则编译器就不编译;
这个DEBUG在哪设置呢,
在 "Target > Build Settings > Preprocessor Macros > Debug" 里有一个"DEBUG=1"。
设置为Debug模式下,Product-->Scheme-->SchemeEdit Scheme
#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