我们知道,每新建立一个工程,比如说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的时候直接用了!

注意点:

1. 一般pch里只放不常变化和修改的东西,如果一股脑儿把乱七八糟的东西都往里面加,其中include的.h里面有发生变化,会导致整个工程重新编译,在大工程(编译几十分钟那种)是比较致命的.

2. 如果只是需要一个全局的头文件来定义一些宏等等,随便选择一个文件名就好了。还有,将每个类用到的头文件放到一个pch里面,这是非常错误的做法,应当在每个类单元引用头文件。

因为pch文件是可以不存在的!

参考资料: http://blog.cnrainbird.com/index.php/2012/03/21/guan_yu_lt_appname_gt_-prefix_pch_wen_jian_de_ke_huan_yong_fa/

Prefix.pch文件的用法的更多相关文章

  1. ios开发 <AppName>-Prefix.pch文件的用法详解

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

  2. XCode6 生成prefix.pch文件

    XCode6里, 新建工程默认是没有pch文件的,苹果取消pch文件这一点肯定有它的道理,刚开始很多人可能不适应,如果我们想使用pch文件,需要手动添加,添加步骤如下:(依旧直接上图)

  3. 关于Xcode6.0.1创建项目不自动创建Prefix.pch文件的解决办法

    1. 新建工程 2. 创建pch文件: 新建文件->Other->PCH File  新建一个pch文件 3. 在setting里面进行设置: 项目配置->Build Setting ...

  4. 生成prefix.pch文件

    (借鉴网络资源)

  5. Info.plist与Prefix.pch修改文件位置遇到的问题及解决方法

    如果要更改Info.plist与Prefix.pch文件实际路径,也就是实际文件的位置(不是在工程中的组织路径),需要到Build Settings中修改对应的配置,不然工程就找不到对应的Info.p ...

  6. iOS开发之pch文件

    项目的Supporting files文件夹下面有个“工程名-Prefix.pch”文件,也是一个头文件 pch头文件的内容能被项目中的其他所有源文件共享和访问 一般在pch文件中定义一些全局的宏 在 ...

  7. iOS PCH文件

    在Xcode6之前,创建一个新的工程,Xcode会再Support Files文件夹下自动创建一个"工程名 - prefix.pch"文件,也是一个头文件,pch文件的内容能被项目 ...

  8. iOS之pch文件的正确使用

    在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一个头文件,pch头文件的内容能被项目中的其他所有源文件 ...

  9. Info.plist和pch文件的作用,UIApplication,iOS程序的启动过程,AppDelegate 方法解释,UIWindow,生命周期方法

    Info.plist常见的设置 建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 注:在旧 ...

随机推荐

  1. 【Leetcode-easy】Remove Nth Node From End of List

    思路1:设置两个指针p1,p2指向表头,p1先走n步.再两个指针同时走.当p1指针指到链表尾部时,P2指针已经在需要删除节点的前一位.一定要注意一些细节. class ListNode { int v ...

  2. linux下安装https证书

    https://www.aliyun.com/jiaocheng/165422.html

  3. 调用微信接口token的问题

    前言 微信的影响力众所周知,越来越多的人也都离不开它,工作,生活,社交的好帮手.相信大家对微信公众号,小程序也都不陌生,那么在开发公众号,小程序的时候需要调用到微信的接口,固然就会遇到token的问题 ...

  4. Android NDK环境搭建

    本文主要记录NDK环境在Ubuntu下的搭建. 下载NDK 在官网进行下载NDK https://developer.android.com/ndk/downloads/index.html 当前最新 ...

  5. javscript 一些常用的工具方法

    一些工作中经常会用到的js代码,可以封装成一个工具库. 积少成多,从现在开始吧! -------------- 1 . 判断一段文字的长度.要求中文相当于2个字符,非中文的相当于1个字符 String ...

  6. c语言学习的第12天

    #include <stdio.h> int main(void) { int *p; int i=5; char ch='A'; p=&i; *p=99; printf(&quo ...

  7. mooc_java Socket

    Socket通信,TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据:基于TCP协议实现网络通信的类客户端的Socket类 服务器端的ServerSocket类 -------------- ...

  8. Linux- 恢复.swp文件

    当我们对Linux文件系统下的文件编辑时,很多新手老手都有可能出现一些失误,在对一个文件编辑或者改动,甚至是不小心按到键盘并没有发现改动到某处时,没有强制退出(:q!)就直接退出,导致文件变成了.sw ...

  9. hihocoder1075【开锁魔法】

    hihocoder1075[开锁魔法] 题意是给你一个 \(1-n\) 的置换,求选 \(k\) 个可以遍历所有点的概率. 题目可以换个模型:有 \(n\) 个球,有 \(cnt\) 种不同的颜色,求 ...

  10. android自定义控件(四) View中的方法

    onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) 确定所有子元素的大小 onLayout(boolean, int, int ...