Prior to Xcode 6, it was popular to use PCH files in projects, throwing in header files and macro definitions to save a lot of code and improve development efficiency. After Xcode 6, Apple removed the PCH file by default. Many people said that PCH slowed down the compilation speed, and Apple removed it for the user experience. And then most people stop using PCH.
In fact, rather than slowing down compilations, PCH generally improves them. The precompiled header files are cached so that recompilations do not need to recompile the imported content from the PCH file, which speeds up compilation.
Question 1: So, why do many people say that PCH slows down compilation?
I think it is because of the wrong way to use, but cause the compilation speed is slow. The main culprit is the introduction of a large number of poorly shared macro definitions and header files. It’s odd that the whole project is looking for and replacing these macro definition fields, repeatedly importing these header files, at compile time.
Correct use:
- Don’t put all of your macro definitions in normal files, especially those that are less common.
- Put common headers in PCH, such as the Fundation framework.
- (Neatness principle) Create a header file for scattered macro definitions and import it into the PCH for easy viewing and modification.
Question 2: What about common macros, frameworks, and tripartite libraries if you don’t use PCH?
The answer is to use inheritance, defining macros in header files of the parent class and importing common frameworks, tripartite libraries. You need something to inherit. Parent classes can be defined separately between layers, depending on the project’s framework. For example, the View layer in MVC, the parent class can add control Category, screen width and macros.
Question 3: Why do most developers avoid using PCH files, even if they know how to use them correctly?
Sometimes macros, frameworks, and tripartite libraries are too common to define and grasp, which can lead to a tangle of states and simply not use. The other biggest disadvantage, I think, is that PCH reduces the portability of your code. Putting frameworks used in your classes into a PCH can be costly if you have to import them again, drag them along, and delete unnecessary code.