Prefix.pch文件的用法

我们知道,每新建立一个工程,比如说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/

时间: 2024-08-06 20:08:17

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

ios开发 &lt;AppName&gt;-Prefix.pch文件的用法详解

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

&lt;AppName&gt;-Prefix.pch文件的添加以及用法

iOS8  xcode6中添加pch全局引用文件 前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6之前的版本建项目就自动添加了是这样的: xcode6后的版本要自己手动的添加步骤如下: 1)  2) 3) $(SRCROOT)/pchFile.pch 这样就ok了! ----------------------------------------------------------------------------------------------

在Xcode6中添加prefix.pch文件

1. 创建prefix.pch文件 2.工程->BuildSettings->prefix END

XCode6 生成prefix.pch文件

XCode6里, 新建工程默认是没有pch文件的,苹果取消pch文件这一点肯定有它的道理,刚开始很多人可能不适应,如果我们想使用pch文件,需要手动添加,添加步骤如下:(依旧直接上图) @ 取消的原因: Stackoverflow上有一个人对此的解释是这样的:       I suspect because of modules, which remove the need for the  #import <Cocoa/Cocoa.h>.       As to where to put c

开源中国iOS客户端学习——(一)Prefix.pch文件

转载自 http://blog.csdn.net/duxinfeng2010/article/details/8287550 当我们新建一个工程的时候,在Supporting FIles文件下会看到一个以  -Prefix.pch结尾文件的文件,pch全称是“precompiled header”,也就是预编译头文件,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器编译速度.我们知道当我 们修改一个工程中某个文件代码时候,编译器并不是重新编译所有所有文件,而

苹果意欲何为?XCode6 舍弃prefix.pch文件

当我们升级到XCode6后, 新建工程发现默认是没有pch文件的,很多人开始不习惯了,苹果究竟为什么要取消这一个pch文件. 苹果认为,由于组件单一模块的原因,你不应该在你的prefix代码中放入任何的代码,把他们放在你确实需要放入的文件中.把你的定义放到属于他们的文件中. 尽量不要使用宏定义(define)..除非是非不得已的时候,(这里 插一句 不使用宏定义的原因是 宏定义是在预编译的时候处理的 因此 当你修改宏定义的时候 会导致大量的代码被重新编译 另外 宏定义存在许多潜在的bug 是因为

xcode 6 创建的demo没有 -Prefix.pch文件

如果pch文件里放太多东西,会导致启动变慢 我们可以自己新建一个: ok!!! 测试:

Prefix.pch文件-判断iOS系统版本

ItcastWeibo-Prefix.pch ////  Prefix header////  The contents of this file are implicitly included at the beginning of every source file.// #import <Availability.h> #ifndef __IPHONE_5_0#warning "This project uses features only available in iOS S

ios之Xcode6手动创建Prefix.pch文件/PCH FileF创建/图文教程

1.先创建PCH File 下图是创建步骤 2.在Save As  输入PCH文件名(标准规范是  项目工程名-Prefix)然后选择Target  点击创建 3.点击工程名--- >TAGETS --> Build Settings   ------------------------->  搜索prefix ------    找到之后双击编辑配置路径   $(SRCROOT)/工程名/pch文件名    然后编译下就行了 图片中pch文件名输入错了是下划线这里纠正下:MG_ROG-