我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch。对于这个文件,很长一段时间里笔者都没觉得它碍事。直到有一天笔者学习NSLog看网上的教程,大家是怎样在最终提交应用的时候,一次性将NSLog语句移除。
网上大多转来转去的方法,都是说把如下的语句

#ifdef DEBUG
#    define DLog(...) NSLog(__VA_ARGS__)
#else
#    define DLog(...) /* */
#endif
#define ALog(...) NSLog(__VA_ARGS__)

加到 <AppName>-Prefix.pch的文件中。虽然对于这种作法,笔者最终由于,不想在调试一个东西而出现一堆东西,最终没有使用这种方法。但是 <AppName>-Prefix.pch这个文件,最终引起了作者的注意。
网上查了一下有解释说.pch是“precompiled header”的意思,那么字面意思理解就是预编译文件头喽。据说在程序编译前都优先编译好这里指定的文件,这样可以加快编译速度。好吧,我们来看看默认这个文件里包含什么:

//
// 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

按着Command键,再点开UIKit/UIKit.h,你发现了什么?你发现了什么?

//
//  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>

举个例子,有没有注意到#import <UIKit/UILabel.h>?笔者在使用如下语句的时候:

UILabel *_testLabel = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 15)];

曾经不止一次的怀疑,这个UILabel是哪来的,为嘛可以直接用。这个文件就说明了一切!
对此你有什么想法?我的想法就是:如果我每个View几乎都要用到ASIHTTPRequest的话,那么我只在这里引用一次ASIHTTPRequest.h就够了!
这样我就可以在需要使用的ASIHTTPRequest的时候直接用了!
于是我工程里的这个文件变成了这样:

//
// 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

这样的话,我觉得的TKAlertCenter神马的,用起来顺手多了,再也不用每次使用的时候先引用一下了。
注意到上面的

  #import "TKAlertCenter.h"
  #define TKDCenter [TKAlertCenter defaultCenter]

没?我觉得每次都写:

[[TKAlertCenter defaultCenter] postAlertWithMessage:@"http://blog.cnrainbird.com"];

这样的句子太长了!有了上面的定义,我现在变成这样写了:

  [TKDCenter postAlertWithMessage:@"http://blog.cnrainbird.com"];

是不是短多了?
同样了,我的每个工程的-Prefix.pch文件里还定义了如下的变量:

#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]

这样做有两个好处:
1.变量名变短了。
虽然object-c里强调变量要完整的表达意思,但是类似[NSUserDefaults standardUserDefaults]这样的单例实在太长了,参数名再长点儿一行都写不下了,很不易于阅读。
2.方便修改
看到DocumentPath这个变量没?大家伙还都记得的IOS5刚出来那会儿,N多app因为把存放文件的路径搁到了Documents下App升级被拒的事儿吧?当时因为我已经用上了这个变量,直接在这个一改,嘿嘿,几十个文件立马跟着变了,省力更省心!

最后发张图,证明我的存在:

ios开发 <AppName>-Prefix.pch文件的用法详解的更多相关文章

  1. IOS开发UI篇之tableView 的用法详解

    1.我们知道tableView是IOS中的高级视图,其继承与ScrollView,故我们知道他有具有ScrollView的所有功能.而且还扩展了许多.当然在这里就不一一介绍了. 2.tableView ...

  2. Prefix.pch文件的用法

    我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...

  3. iOS开发之--为PCH文件添加绝对路径

    要想设置PCH的相对路径,首先我们需要去查看绝对路径. 相对路径 点击PCH文件,Xcode的右侧会显示PCH的属性.这里我们可以获取到PCH的绝对路径.从工程的路径开始,前面使用$(SRCROOT) ...

  4. iOS 开发中有关pch文件,以及pch常用的内容

    一.创建pch文件.点击command+N.如下图操作 命名规则一般都是:项目名称-Prefix 第二步 OK,到这里已经把pch文件制作完毕.接下来就可以用了. pch文件一般书写的是全局都可以用到 ...

  5. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

  6. C++通过jsoncpp类库读写JSON文件-json用法详解

    介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...

  7. ios开发之--armv7,armv7s,arm64,i386,x86_64详解

    有时候在运行的时候,经常出现诸如i386的错误,最新一些可能会出现 No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch ...

  8. QT .pro文件 LIBS用法详解

    在程序中需要使用到团队其它成员开发的静态库和动态库,起初是知道使用LIBS变量在在.pro文件中指定需要包含的库,但是实际使用的时候却遇到很大麻烦,但其实确实是因为自己看官方文档不太用心造成的. 下面 ...

  9. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

随机推荐

  1. sass,compass让开发效率飞起

    最近开始学习并且使用,发现使用它写起css来真的是各种爽 安装sass,compass sass是依赖于ruby的,必须先安装Ruby,点击下载 下载完ruby之后,使用命令行安装sass       ...

  2. 如何使用impress.js做一个网页版本的PPT

    blockquote{font-size: 18px;line-height:1.5;margin:0;}line-height: 1.5; 要做一个网站制作规范培训,之前村长做过一次培训,但是后来一 ...

  3. Zebra_Dialog 弹出层插件

    . Default dialog box, no custom settings. Click here to open. $.Zebra_Dialog('<strong>Zebra_Di ...

  4. [设计模式] javascript 之 建造者模式

    建造者模式说明 1. 将一个复杂对象的 构造 与它的表示相分离,使同样的创建过程可有不同的表示,这就叫做建造者模式. 2. 面向对象语言中的说明,主要角色: 1>. Builder 这个接口类, ...

  5. 每天一个linux命令(35):killall命令

    Linux 系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进 程,如果要找到我们需要杀死的进程,我们还需 ...

  6. 转-JS中document对象详解

    对象属性 document.title //设置文档标题等价于HTML的<title>标签 document.bgColor //设置页面背景色 document.fgColor //设置 ...

  7. Java_HttpURLConnection使用

    包括使用HttpURLConnection执行get/post请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  8. Ajax 的缺点

    1.ajax干掉了back按钮,即对浏览器后退机制的破坏.后退按钮是一个标准的web站点的重要功能,但是它没法和js进行很好的合作.这是ajax所带来的一个比较严重的问题,因为用户往往是希望能够通过后 ...

  9. Java-编写一个jdbc操作类

    1.通过读取文件配置 package 数据库操作类; /* * Db.java Created on 2007年8月20日, 上午 8:37 */ import java.io.*; import j ...

  10. Java基础-被final修饰的引用变量的指向

    final修饰的引用变量一旦初始化赋值之后就不能再指向其他的对象,那么该引用变量指向的对象的内容可变吗?看下面这个例子: public class Test { public static void ...