PCH file for iOS learning,

Source: Internet
Author: User

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
  • Store some common macros

  • Stores some common header files, such as header files or classified header files of the Chinese tool class.

  • Manage log output

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.