-Prefix. PCH File Usage

Source: Internet
Author: User

We know that every time you create a new project, such as helloword, there will be a file starting with the project name-prefix. PCH in category suppingingfiles, such as a HelloWord-Prefix.pch. For this file, I haven't thought it was in the way for a long time. One day I learned about nslog and read the online tutorials. How do you remove the nslog statement at one time when submitting the application.
Most of the online transfer methods refer to the following statements:

# Ifdef debug
# Define dlog (...) nslog (_ va_args __)
# Else
# Define dlog (...)/**/
# Endif
# Define alog (...) nslog (_ va_args __)


Add it to the <appname>-prefix. PCH file. Although for this practice, I finally did not use this method because I didn't want to debug a bunch of things. However, the <appname>-prefix. PCH file eventually attracted the attention of the author.
I checked it online and explained that. PCH is the meaning of "precompiled header", so the literal meaning is the precompiled file header. It is said that the files specified here are compiled first before the program compilation, which can speed up compilation. Okay. Let's take a look at what is contained in the default file:

//
// Prefix header for all source files of the 'helloworld' target in the 'helloworld' Project
//

# Import <availability. h>

# Ifndef _ iphone_4_0
# Warning "This project uses features only available in IOS sdks 4.0 and later ."
# Endif

# Ifdef _ objc __
# Import <uikit/uikit. h>
# Import <Foundation/Foundation. h>
# Endif

Press the command key and click uikit/uikit. H. What did you find? What did you find?

//
// Uikit. h
// Uikit
//
// Copyright (c) 2005-2011, Apple Inc. All rights reserved.
//

# Import <uikit/uikitdefines. h>
# Import <uikit/uiaccelerometer. h>
# Import <uikit/uiaccessibility. h>
# Import <uikit/uiactivityindicatorview. h>
# Import <uikit/uialert. h>
# Import <uikit/uiapplication. h>
# Import <uikit/uibarbuttonitem. h>
# Import <uikit/uibaritem. h>
# Import <uikit/uibezierpath. h>
# Import <uikit/uibutton. h>
# Import <uikit/uicolor. h>
# Import <uikit/uicontrol. h>
# Import <uikit/uidatadetectors. h>
# Import <uikit/uidatepicker. h>
# Import <uikit/uidevice. h>
# Import <uikit/uidocument. h>
# Import <uikit/uidocumentinteractioncontroller. h>
# Import <uikit/uievent. h>
# Import <uikit/uifont. h>
# Import <uikit/uigeometry. h>
# Import <uikit/uigesturerecognizer. h>
# Import <uikit/uigraphics. h>
# Import <uikit/uiimage. h>
# Import <uikit/uiimagepickercontroller. h>
# Import <uikit/uiimageview. h>
# Import <uikit/uiinterface. h>
# Import <uikit/uilabel. h>
# Import <uikit/uilocalnotification. h>
# Import <uikit/uilocalizedindexedcollation. h>
# Import <uikit/uilongpressgesturerecognizer. h>
# Import <uikit/uimanageddocument. h>
# Import <uikit/uimenucontroller. h>
# Import <uikit/uinavigationbar. h>
# Import <uikit/uinavigationcontroller. h>
# Import <uikit/uinib. h>
# Import <uikit/uinibdeclarations. h>
# Import <uikit/uinibloading. h>
# Import <uikit/uipagecontrol. h>
# Import <uikit/uipageviewcontroller. h>
# Import <uikit/uipangesturerecognizer. h>
# Import <uikit/uipasteboard. h>
# Import <uikit/uipickerview. h>
# Import <uikit/uipinchgesturerecognizer. h>
# Import <uikit/uipopovercontroller. h>
# Import <uikit/uipopoverbackgroundview. h>
# Import <uikit/uiprinterror. h>
# Import <uikit/uiprintformatter. h>
# Import <uikit/uiprintinfo. h>
# Import <uikit/uiprintinteractioncontroller. h>
# Import <uikit/uiprintpagerenderer. h>
# Import <uikit/uiprintpaper. h>
# Import <uikit/uiprogressview. h>
# Import <uikit/uireferencelibraryviewcontroller. h>
# Import <uikit/uiresponder. h>
# Import <uikit/uirotationgesturerecognizer. h>
# Import <uikit/uiscreen. h>
# Import <uikit/uiscreenmode. h>
# Import <uikit/uiscrollview. h>
# Import <uikit/uisearchbar. h>
# Import <uikit/uisearchdisplaycontroller. h>
# Import <uikit/uisegmentedcontrol. h>
# Import <uikit/uislider. h>
# Import <uikit/uisplitviewcontroller. h>
# Import <uikit/uistepper. h>
# Import <uikit/uistoryboard. h>
# Import <uikit/uistoryboardpopoversegue. h>
# Import <uikit/uistoryboardsegue. h>
# Import <uikit/uistringdrawing. h>
# Import <uikit/uiswipegesturerecognizer. h>
# Import <uikit/uiswitch. h>
# Import <uikit/uitabbar. h>
# Import <uikit/uitabbarcontroller. h>
# Import <uikit/uitabbaritem. h>
# Import <uikit/uitableview. h>
# Import <uikit/uitableviewcell. h>
# Import <uikit/uitableviewcontroller. h>
# Import <uikit/uitapgesturerecognizer. h>
# Import <uikit/uitextfield. h>
# Import <uikit/uitextinput. h>
# Import <uikit/uitextinputtraits. h>
# Import <uikit/uitextview. h>
# Import <uikit/uitoolbar. h>
# Import <uikit/uitouch. h>
# Import <uikit/uivideoeditorcontroller. h>
# Import <uikit/uiview. h>
# Import <uikit/uiviewcontroller. h>
# Import <uikit/uiwebview. h>
# Import <uikit/uiwindow. h>

For example, have you noticed # import <uikit/uilabel. h>? When using the following statement:

Uilabel * _ testlabel = [uilabel alloc] initwithframe: cgrectmake (0, 0, 20, 15)];

I have been skeptical more than once about where this uilabel came from. why can it be used directly. This file shows everything!
What do you think about this? My idea is: If I use asihttprequest for almost every view, I only need to reference asihttprequest. h here!
In this way, I can use asihttprequest directly!
So the file in my project becomes like this:

//
// Prefix header for all source files of the 'helloworld' target in the 'helloworld' Project
//

# Import <availability. h>

# Ifndef _ iphone_3_0
# Warning "This project uses features only available in iPhone sdks 3.0 and later ."
# Endif

# Ifdef _ objc __
# Import <uikit/uikit. h>
# Import <Foundation/Foundation. h>
# Import "nstimer. H"
# Import "buttwithblockactions. H"
# Import "tkalertcenter. H"
# Define tkdcenter [tkalertcenter defaultcenter]
# Import "datamanager. H"
# Define ddmanager [datamanager defaultmanager]
# Define datamemdic [ddmanager memdic]
// For checkupdateversion
# Import "reachability. H"
# Import "asihttprequest. H"
# Import "asiformdatarequest. H"
# Import "JSON. H"

// Blockalert
# Import "ributtonitem. H"
# Import "uialertview + blocks. H"
# Import "uiactionsheet + blocks. H"

# Import <quartzcore/quartzcore. h>
# Import "function. H"
# Import "mediaplayer/mediaplayer. H"
# Endif

In this case, I think the tkalertcenter is much easier to use and I don't need to reference it every time.
Note that

# Import "tkalertcenter. H"
# Define tkdcenter [tkalertcenter defaultcenter]

No? I think every time I write:

[[Tkalertcenter defaultcenter] postalertwithmessage: @ "http://blog.cnrainbird.com"];

This sentence is too long! With the above definition, I have written it like this:

[Tkdcenter postalertwithmessage: @ "http://blog.cnrainbird.com"];

Is it too short?
Similarly, the following variables are defined in the-prefix. PCH file of each of my projects:

# Define ndsud [nsuserdefaults standarduserdefaults]
# Define nncdc [nsicationicationcenter defacenter center]
# Define FM [nsfilemanager defamanager manager]
# Define appenders app [uiapplication sharedapplication]
# Define apporientation [[uiapplication sharedapplication] statusbarorientation]
# Define documentpath [nsstring stringwithformat: @ "% @/library/caches", nshomedirectory ()]
# Define iossystemversion [[[uidevice currentdevice] systemversion] floatvalue]

There are two advantages:
1. The variable name is shortened.
Although the variables in object-C should be fully expressed, the singleton like [nsuserdefaults standarduserdefaults] is too long, and the parameter name cannot be written for a long line, it is not easy to read.
2. Convenient Modification
Do you see the variable documentpath? When the ios5 came out, N-plus apps put the path for storing files in the rejected documents, right? At that time, because I already used this variable, I directly changed it. Hey hey, dozens of files immediately changed, saving effort and worry!

The last screenshot proves my existence:

Reprinted Please note:Transferred from the personal blog of Rainbird

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.