在Xcode6之前,新建一個工程的時候,系統(tǒng)會幫我們自動新建一個以工程名為名字的pch (precompile header)文件,在開發(fā)過程中,可以將那些整個工程都廣泛使用的頭文件包含在該文件下,編譯器就會自動的將pch文件中的頭文件添加到所有的源文件中去,這樣在需要使用相關類的時候不需要使用import就可以直接使用頭文件中的內(nèi)容,很大程度上帶來了編程的便利性,但潛在的也帶來了一些問題,這也是在Xcode6中默認不再創(chuàng)建pch的原因吧。
關于pch的得與失,stackoverflow上有段話講的比較透徹:http://stackoverflow.com/questions/24158648/why-isnt-projectname-prefix-pch-created-automatically-in-xcode-6
As to where to put code that you would put in a prefix header, there is no code you should put in a prefix header. Put your imports into the files that need them. Put your definitions into their own files. Put your macros...nowhere. Stop writing macros unless there is no other way (such as when you need __FILE__). If you do need macros, put them in a header and include it.
The prefix header was necessary for things that are huge and used by nearly everything in the whole system (like Foundation.h). If you have something that huge and ubiquitous, you should rethink your architecture. Prefix headers make code reuse hard, and introduce subtle build problems if any of the files listed can change. Avoid them until you have a serious build time problem that you can demonstrate is dramatically improved with a prefix header.
In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.
但有些時候,還是需要pch文件的,那么怎么在Xcode6中添加一個pch文件呢?
步驟:
首先,Command+N,打開新建文件窗口:ios->other->PCH file,創(chuàng)建一個pch文件,添加需要引入的頭文件名:
其次,修改工程配置文件,點擊 項目名稱->Build Setting中搜索“prefix”:如圖
然后:Apple LLVM 6.0 -Language中得“Prefix Header”中 輸入你新建的pch文件的路徑。如圖:
最后輸入:$(SRCROOT)/MyAutoLayoutDemo/XXX.pch
注意:(這里可以直接將你新建的pch文件之間拖入該位置,就是現(xiàn)實的文件的絕對位置,但是不具有移植性; $(SRCROOT)就是代表你的項目在你文件的絕對路徑,因此僅需要輸入xxx.pch在你項目的相對路徑就行)?
我的pch路徑:
切記:路徑的一定要正確,否則會無法找到,編譯不通過。
注:目前在Xcode6.1的版本好使, 但是xcode6.2目前不穩(wěn)定,暫時不能使用。