PCH file for iOS learning,
1. Overview of PCH files
A pch file is a pre-compiled header file (generally with the extension. PCH) That precompiles stable code in a project and stores it in a file (. PCH. These pre-compiled codes can be any C/C ++ code-or even inline functions, but they are relatively stable throughout the project, that is, the code that is not frequently modified during the project development process.
In versions earlier than Xcode6.0, the generated Single View Application project automatically contains the PCH file. After Xcode6.0, pch is removed. For some trivial header file references, the Compilation speed is accelerated!
For Xcode5 projects:
Versions earlier than Xcode6.0)
In the project, command + N-> Other-> PCH File-> Next-> Create
The PCH file is compiled in advance, so we need to tell the project. Specific operations are as follows:
Of course, if you want to fill in, you can use $ (SRCROOT) to obtain the path of your project file. The complete code can be written as: $ (SRCROOT)/PCHTest/header. pch
3. Functions of PCH files
Custom Log: # define ZFLog (...) NSLog (_ VA__ARGS __)
Variable parameters in the macro :...
Variable Parameter in the function: __va__args __
Log output is very performance-consuming. Generally, log files are not required for release and are only required for debugging.
We have two simple ways to eliminate all log output in the project when releasing and releasing the product.
① We can directly comment on the # define ZFLog (...) // NSLog (_ VA__ARGS _) to eliminate all log output in the project
② You can also use a macro to perform Conditional compilation. In the debugging phase, xcode will automatically define a DEBUG macro and use this macro to perform Conditional compilation.
# Ifdef DEBUG // debugging phase # define ZFLog (...) NSLog (_ VA__ARGS _) # else // release phase # define ZFLog (...) # Endif
4. precautions for using the PCH file for OC-related items written in PCH, it is best to put it in # ifdef _ OBJC _. Xcode defines this macro in every OC file, this means that only files in the OC have these macros, avoiding errors when there are C files in the project. For example, if # import is used in the c file, an error is returned. Because the c file cannot recognize # import, only the OC file can recognize it. Items in PCH can be shared by all files in the project. C file sharing # import reports an error.
# Correct syntax of ifdef _ OBJC _/OC file # import <UIKit/UIKit. h> # import <Foundation/Foundation. h> # endif # import "ZFTool. h "// an error may be reported here, so do not write it here