在release(发布app)中屏蔽NSLog输出语句

来源:互联网 发布:淘宝新店第一天刷几单 编辑:程序博客网 时间:2024/05/17 22:04

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

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

第一种:

  1. #ifndef __OPTIMIZE__   
  2. #define NSLog(...) NSLog(__VA_ARGS__)   
  3. #else   
  4. #define NSLog(...) {}   
  5. #endif

第二种:

  1. #ifndef __IPHONE_4_0   
  2. #warning "This project uses features only available in iOS SDK 4.0 and later."   
  3. #endif   
  4.   
  5. #ifdef __OBJC__   
  6.     #import <UIKit/UIKit.h>   
  7.     #import <Foundation/Foundation.h>   
  8.     #import "AppDelegate.h"   
  9.     #import "UIViewController+Customized.h"   
  10. #endif


            #ifndef __OPTIMIZE__   
  1. #define NSLog(...) NSLog(__VA_ARGS__)   
  2. #else   
  3. #define NSLog(...) {}   
  4. #endif  
0 0