-Prefix.pch是什么?
当我们新建一个工程的时候,在Supporting FIles文件下会看到一个以 -Prefix.pch结尾文件的文件,pch全称是“precompiled header”,也就是预编译头文件,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器的编译速度。我们知道当我们修改一个工程中某个文件代码时候,编译器并不是重新编译所有所有文件,而是编译改动过文件的,假如pch中某个文件修改了,那么pch整个文件里包含的的其他文件也会重新编译一次,这样就会消耗大量时间,所以它里面添加的文件最好是是很少变动或不变动的头文件或者是预编译的代码片段。
出现这种情况,可能就是-Prefix.pch文件做了修改,重新编译了。
新建一个工程,pch后缀文件里代码:
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
或许你会觉得这预编译代码很少,你可以查看一下
<UIKit/UIKit.h>的定义文件中。(前者为框架名称,后者为框架内的类文件,该类文件涵盖该框架所有类)
再举个完整一点的例子
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
//添加的预编译
#import "ARCSingletonTemplate.h"
#import "UIView+block.h"
#import "UIControl+block.h"
#import "UtilsMacro.h"
#import "NSUserDefaults+Helpers.h"
#import <QuartzCore/QuartzCore.h>
#import "XCCommonUtility.h"
//api定义
#define api_news_list @"http://www.oschina.net/action/api/news_list"
#define api_post_list @"http://www.oschina.net/action/api/post_list"
#define api_blog_list @"http://www.oschina.net/action/api/blog_list"
//宏定义 新闻
#define TweetCellIdentifier @"TweetCellIdentifier"
#define FavoriteCellIdentifier @"FavoriteCellIdentifier"
#define loadNext20Tip @"下面 20 项 . . ."
#define loadingTip @"正在加载 . . ."
//消息通知固定字符串
#define Notification_DetailCommentCount @"Notification_DetailCommentCount"
#define Notification_NoticeUpdate @"Notification_NoticeUpdate"
#define Notification_TabClick @"Notification_TabClick"
//html头部
#define HTML_Style @"<style>#oschina_title {color: #000000; margin-bottom: 6px; font-weight:bold;}#oschina_title img{vertical-align:middle;margin-right:6px;}#oschina_title a{color:#0D6DA8;}#oschina_outline {color: #707070; font-size: 12px;}#oschina_outline a{color:#0D6DA8;}#oschina_software{color:#808080;font-size:12px}#oschina_body img {max-width: 300px;}#oschina_body {font-size:16px;max-width:300px;line-height:24px;} #oschina_body table{max-width:300px;}#oschina_body pre { font-size:9pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;}</style>"
#define HTML_Bottom @"<div style='margin-bottom:60px'/>"
#define USERAGENT @"OSChina.NET/iOS/5.0"
#define AppVersion @"1.6.1"
#ifdef DEBUG
#define debugLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define debugLog(...)
#define debugMethod()
#endif
#endif
NSLog的使用
#ifdef DEBUG
#define Mylog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define Mylog(...)
#define debugMethod()
#endif
在XCode做开发调试时往往需要打印一些调试信息做debug用,大家知道当打印信息的地方多了之后在模拟器上跑可能不会有什么问题,因为模拟器用的是电脑的硬件但是当应用跑在设备上时这些输出语句会在很大程度上影响应用的性能,针对这种问题可以写一些宏来控制这些调试信息的输出。
因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉。
我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时,再取消这些注释,这实在是一件无趣而耗时的事!还好,还有更优雅的解决方法,就是在项目的prefix.pch文件里加入下面一段代码,加入后,NSLog就只在Debug下有输出,Release下不输出了。
上段代码的意思就是 用宏指令做一个判断,如果DEBUG为真,则编译#ifdef到#endif宏定义,否则编译器就不编译;
这个DEBUG在哪设置呢。
点击左上角的工程target名称,下拉菜单中的Edite Schemes,里面可以设置对应不同功能(运行调试“Run”、内存检测“Profile”、打包“Archeve”等)的Build Configuration。
在 "Target > Build Settings > Preprocessor Macros > Debug" 里有一个"DEBUG=1"。
设置为Debug模式下,Product-->Scheme-->SchemeEdit Scheme一、"Debug是调试版本,包括的程序信息更多"
二、只有DEBUG版的程序才能设置断点、单步执行、使用TRACE/ASSERT等调试输出语句。
三、REALEASE不包含任何调试信息,所以体积小、运行速度快。