An explanation of the use of iOS development <appname>-prefix.pch files

Source: Internet
Author: User

We know that every new project, such as Helloword, will have a file in the classification supportingfiles that ends with the-prefix.pch of the project name, such as HELLOWORD-PREFIX.PCH. For a long time, I didn't think it was in the way of this file. Until one day I learned nslog to see the online tutorial, how everyone in the final submission of the application, once the NSLog statement is removed.
Most of the online approach is to say the following statements

#ifdef DEBUG
# define DLOG (...) NSLog (__va_args__)
#else
# define DLOG (...)/* */
#endif
#define ALOG (...) NSLog (__va_args__)


Added to the <appname>-prefix.pch file. Although for this kind of practice, the author finally because, does not want to debug a thing to appear a bunch of things, finally did not use this method. But <appname>-prefix.pch the document, which eventually drew the attention of the author.
I looked it up on the Internet. PCH is the "precompiled header" meaning, then the literal meaning is precompiled file header. It is said that the files specified here are first compiled before the program is compiled, which speeds up the compilation speed. Okay, let's see what the default file contains:

//
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 SDK 4.0 and later."
#endif

#ifdef __objc__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif

Press the command key, then open the uikit/uikit.h, what did you find? What did you find out?

//
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 I use the following statement:

UILabel *_testlabel = [UILabel alloc] Initwithframe:cgrectmake (0, 0, 20, 15)];

Once more than once suspicion, this uilabel is where, for why can directly use. This file will explain everything!
What do you think about that? My idea is: if I almost use every view of asihttprequest, then I only quote once ASIHTTPRequest.h is enough!
So I can use it in the asihttprequest when I need it!
So this file in my project becomes 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 SDK 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 way, I think the tkalertcenter god horse, use more handy, and no need to use each time the first quote.
Notice that the above

#import "TKAlertCenter.h"
#define Tkdcenter [Tkalertcenter Defaultcenter]

Didn't? I think it's written every time:

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

Such a sentence is too long! With the definition above, I have now become so written:

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

Is it much shorter?
Similarly, the following variables are defined in the-prefix.pch file for each of my projects:

#define NDSUD [Nsuserdefaults Standarduserdefaults]
#define NNCDC [Nsnotificationcenter Defaultcenter]
#define FM [Nsfilemanager Defaultmanager]
#define APPSHAREAPP [UIApplication sharedapplication]
#define Apporientation [[UIApplication sharedapplication] statusbarorientation]
#define DOCUMENTPATH [NSString stringwithformat:@ "%@/library/caches", Nshomedirectory ()]
#define Iossystemversion [[[Uidevice Currentdevice] systemversion] floatvalue]

There are two benefits to doing so:
1. The variable name is shortened.
Although the OBJECT-C stress variable to complete the expression of meaning, but similar to [nsuserdefaults Standarduserdefaults] Such a single case is too long, the name of the parameter is longer a line is not written, it is not easy to read.
2. Easy to modify
See documentpath this variable? The big guys still remember the IOS5 just came out, n many apps because the path of storing files to the document under the app upgrade is rejected? At that time because I have used this variable, directly in this change, hey, dozens of files immediately followed the change, labor-saving more worry!

NOTE: reprint please specify: transfer from Rainbird's personal blog

An explanation of the use of iOS development <appname>-prefix.pch files

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.