iOS pch文件的作用和用法(附:在release版本禁止输出NSLog内容)

一. pch文件的作用和用法(Xcode > 4.0)
pch全称是“precompiled header”,也就是预编译头文件。
开发的过程中可以将广泛使用的头文件以及宏包含在该文件下,编译器会自动将pch文件中的头文件,添加到所有的源文件中去。这样在需要使用相关类的时候不需要使用import就可以直接使用头文件中的内容,很大程度上带来了编程的便利性。
但是在Xcode6中去掉Precompile Prefix Header文件,主要原因可能在于Prefix Header大大的增加了Build的时间。
缺点:
当我们修改一个工程中某个文件代码时候,编译器并不是重新编译所有所有文件,而是编译改动过文件;假如pch中某个文件修改了,那么pch整个文件里包含的的其他文件也会重新编译一次,这样就会消耗大量时间。



二. 手动添加pch文件
1.  创建pch文件
cmd + n  -> other -> PCH File
2.  将它与编译设置关联(修改工程配置文件)
Build setting中搜索 prefix header, 添加path 
$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch
其中:
$(SRCROOT):代表工程路径
$(PROJECT_NAME):代表项目名称
PrefixHeader.pch : 创建的pch文件名
3.  Precompile Prefix Header
如果Precompile Prefix Header为YES,那么pch会被预编译,预编译后的pch文件会被缓存起来,从而提高编译速度。
如果Precompile Prefix Header为NO,那么pch不会被预编译,而是在每一个用到它导入的框架类库的.m文件中编译一次,降低了编译速度。



三.  使用 ---- 该文件里存放的工程中一些不常被修改的代码
1.  常用的宏定义
#define ScreenWidth  ([[UIScreen mainScreen] bounds].size.width)
#define ScreenHeight ([[UIScreen mainScreen] bounds].size.height)

#define iOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
#define iOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define iOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define iOS6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)


2.   Release版去掉所有的NSLog(release模式通常会定义 __OPTIMIZE__,debug模式不会)
#ifndef __OPTIMIZE__
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...) {}
#endif


猜你喜欢

转载自blog.csdn.net/RangingWon/article/details/53890646