在Xcode6以前的版本中创建工程会自动创建一个pch文件,而Xcode6版本中,系统取消了创建pch,下面是创建方法:
1.新建文件-选择Other-pch文件;
2.填写pch文件的名字,可按照以前系统自动命名的规则:工程名-Prefix.pch;
3.修改工程配置文件BuildSettings,可搜索prefix Header找到,添加路径:工程名/工程名-Prefix.pch
4.编译一下工程,就可以使用了。
5.配置pch里应配置的代码;
pch里有两句配置代码:
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
所以需要添加代码:#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
重点:如果不添加,自定义的类使用UIKit和Foundation里的内容,会不识别报错。
本人的一点小建议:之前见有开发者经常在pch里添加头文件使用,这样虽然方便了编程的便利,但是也存在了一些潜在问题,这也有可能是Xcode6不在默认创建pch的原因,所以建议开发者不要在pch里添加头文件,在哪里文件需要引入头文件时import.