Prefix.pch文件的作用
转自http://blog.csdn.net/duxinfeng2010
考虑到还会有很多童鞋会看到该Blog,在此做一个说明,开源中国iOS开源客户端源码已做重构,请下载最新的源码学习。
新repo地址:http://git.oschina.net/oschina/iphone-app。
另外,这篇Blog收集了一些其他社区的客户端源码,源码也正在不断更新中,有的也上架appstore。
访问地址:http://duxinfeng.com/2015/07/14/iOS%E5%BC%80%E6%BA%90App%E6%95%B4%E7%90%86/
---------------------------------------------------------------------------------------------------------------------------
当我们新建一个工程的时候,在Supporting FIles文件下会看到一个以 -Prefix.pch结尾文件的文件,pch全称是“precompiled header”,也就是预编译头文件,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器编译速度。我们知道当我们修改一个工程中某个文件代码时候,编译器并不是重新编译所有所有文件,而是编译改动过文件的,假如pch中某个文件修改了,那么pch整个文件里包含的的其他文件也会重新编译一次,这样就会消耗大量时间,所以它里面添加的文件最好是是很少变动或不变动的头文件或者是预编译的代码片段;
在新建一个工程时,pch后缀文件里代码是
- #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
或许你会觉得这预编译代码很少,但是你可以查看一下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>
这些不少了吧,工程每次运行都编译是不是很费时间,这些是苹果公司内部定义的标准头文件,我们不能也没有权限修改这些头文件定义内容,所以,当放到pch文件中会加速编译过程;
再来看看我们开源中国iOS客户端pch文件
- //
- // Prefix header for all source files of the 'oschina' target in the 'oschina' 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>
- #import <CoreData/CoreData.h>
- #import <QuartzCore/QuartzCore.h>
- //添加的预编译
- #import "ASIHTTPRequest.h"
- #import "ASIFormDataRequest.h"
- #import "ASIHTTPRequestDelegate.h"
- #import "ASIHTTPRequestConfig.h"
- #import "TBXML.h"
- #import "TBXML+HTTP.h"
- #import "TBXML+Compression.h"
- #import "Config.h"
- #import "EGORefreshTableHeaderView.h"
- #import "DataSingleton.h"
- #import "ImgRecord.h"
- #import "IconDownloader.h"
- #import "MBProgressHUD.h"
- #import "GCDiscreetNotificationView.h"
- #import "NdUncaughtExceptionHandler.h"
- #import "JSNotifier.h"
- #import "AFOSCClient.h"
- #import "AFHTTPRequestOperation.h"
- #import "AFXMLRequestOperation.h"
- //api定义
- #define api_news_list @"http://www.oschina.net/action/api/news_list"
- #define api_news_detail @"http://www.oschina.net/action/api/news_detail"
- #define api_post_list @"http://www.oschina.net/action/api/post_list"
- #define api_post_detail @"http://www.oschina.net/action/api/post_detail"
- #define api_post_pub @"http://www.oschina.net/action/api/post_pub"
- #define api_tweet_list @"http://www.oschina.net/action/api/tweet_list"
- #define api_tweet_detail @"http://www.oschina.net/action/api/tweet_detail"
- #define api_tweet_delete @"http://www.oschina.net/action/api/tweet_delete"
- #define api_tweet_pub @"http://www.oschina.net/action/api/tweet_pub"
- #define api_active_list @"http://www.oschina.net/action/api/active_list"
- #define api_message_list @"http://www.oschina.net/action/api/message_list"
- #define api_message_delete @"http://www.oschina.net/action/api/message_delete"
- #define api_message_pub @"http://www.oschina.net/action/api/message_pub"
- #define api_comment_list @"http://www.oschina.net/action/api/comment_list"
- #define api_comment_pub @"http://www.oschina.net/action/api/comment_pub"
- #define api_comment_reply @"http://www.oschina.net/action/api/comment_reply"
- #define api_comment_delete @"http://www.oschina.net/action/api/comment_delete"
- #define api_login_validate @"https://www.oschina.net/action/api/login_validate"
- #define api_user_info @"http://www.oschina.net/action/api/user_info"
- #define api_user_information @"http://www.oschina.net/action/api/user_information"
- #define api_user_updaterelation @"http://www.oschina.net/action/api/user_updaterelation"
- #define api_notice_clear @"http://www.oschina.net/action/api/notice_clear"
- #define api_software_detail @"http://www.oschina.net/action/api/software_detail"
- #define api_blog_detail @"http://www.oschina.net/action/api/blog_detail"
- #define api_favorite_list @"http://www.oschina.net/action/api/favorite_list"
- #define api_favorite_add @"http://www.oschina.net/action/api/favorite_add"
- #define api_favorite_delete @"http://www.oschina.net/action/api/favorite_delete"
- #define api_user_notice @"http://www.oschina.net/action/api/user_notice"
- #define api_search_list @"http://www.oschina.net/action/api/search_list"
- #define api_friends_list @"http://www.oschina.net/action/api/friends_list"
- #define api_softwarecatalog_list @"http://www.oschina.net/action/api/softwarecatalog_list"
- #define api_software_list @"http://www.oschina.net/action/api/software_list"
- #define api_softwaretag_list @"http://www.oschina.net/action/api/softwaretag_list"
- #define api_blogcomment_list @"http://www.oschina.net/action/api/blogcomment_list"
- #define api_blogcomment_pub @"http://www.oschina.net/action/api/blogcomment_pub"
- #define api_my_information @"http://www.oschina.net/action/api/my_information"
- #define api_blogcomment_delete @"http://www.oschina.net/action/api/blogcomment_delete"
- #define api_userblog_delete @"http://www.oschina.net/action/api/userblog_delete"
- #define api_userblog_list @"http://www.oschina.net/action/api/userblog_list"
- #define api_blog_list @"http://www.oschina.net/action/api/blog_list"
- #define api_userinfo_update @"http://www.oschina.net/action/api/portrait_update"
- //宏定义 新闻
- #define TweetCellIdentifier @"TweetCellIdentifier"
- #define loadMoreIdentifier @"loadMoreIdentifier"
- #define NewsCellIdentifier @"NewsCellIdentifier"
- #define PostCellIdentifier @"PostCellIdentifier"
- #define MsgCellIdentifier @"MsgCellIdentifier"
- #define MsgUnitCellIdentifier @"MsgUnitCellIdentifier"
- #define ActiveCellIdentifier @"ActiveCellIdentifier"
- #define UserActiveCellIdentifier @"UserActiveCellIdentifier"
- #define ColorActiveCellIdentifier @"ColorActiveCellIdentifier"
- #define RTActiveCellIdentifier @"RTActiveCellIdentifier"
- #define ColorUserActiveCellIdentifier @"ColorUserActiveCellIdentifier"
- #define ProfielCellIdentifier @"ProfielCellIdentifier"
- #define CommentCellIdentifier @"CommentCellIdentifier"
- #define NormalCellIdentifier @"NormalCellIdentifier"
- #define FavoriteCellIdentifier @"FavoriteCellIdentifier"
- #define FriendCellIdentifier @"FriendCellIdentifier"
- #define SoftwareCellIdentifier @"SoftwareCellIdentifier"
- #define SoftwareCatalogIdentifier @"SoftwareCatalogIdentifier"
- #define SettingTableIdentifier @"SettingTableIdentifier"
- #define MyInfoCellIdentifier @"MyInfoCellIdentifier"
- #define MyPortraitCellIdentifier @"MyPortraitCellIdentifier"
- #define loadNext20Tip @"下面 20 项 . . ."
- #define loadingTip @"正在加载 . . ."
- #define networkError @"网络无连接"
- #define noNetworkTip @"网络无连接"
- //消息通知固定字符串
- #define Notification_DetailCommentCount @"Notification_DetailCommentCount"
- #define Notification_NoticeUpdate @"Notification_NoticeUpdate"
- #define Notification_TabClick @"Notification_TabClick"
- //html头部
- #define HTML_Style @"<style>#oschina_title {color: #000000; margin-bottom: 6px; font-weight:bold;}#oschina_title img{vertical-align:middle;margin-right:6px;}#oschina_title a{color:#0D6DA8;}#oschina_outline {color: #707070; font-size: 12px;}#oschina_outline a{color:#0D6DA8;}#oschina_software{color:#808080;font-size:12px}#oschina_body img {max-width: 300px;}#oschina_body {font-size:16px;max-width:300px;line-height:24px;} #oschina_body table{max-width:300px;}#oschina_body pre { font-size:9pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;}</style>"
- #define HTML_Bottom @"<div style='margin-bottom:60px'/>"
- #define USERAGENT @"OSChina.NET/iOS/5.0"
- #define AppVersion @"1.6.1"
- #ifdef DEBUG
- #define debugLog(...) NSLog(__VA_ARGS__)
- #define debugMethod() NSLog(@"%s", __func__)
- #else
- #define debugLog(...)
- #define debugMethod()
- #endif
- #endif
我们看到有这样些文件也被添加到里面,可能会想难道这些头文件变化不大吗?
- //添加的预编译
- #import "ASIHTTPRequest.h"
- #import "ASIFormDataRequest.h"
- #import "ASIHTTPRequestDelegate.h"
- #import "ASIHTTPRequestConfig.h"
- #import "TBXML.h"
- #import "TBXML+HTTP.h"
- #import "TBXML+Compression.h"
- #import "Config.h"
- #import "EGORefreshTableHeaderView.h"
- #import "DataSingleton.h"
- #import "ImgRecord.h"
- #import "IconDownloader.h"
- #import "MBProgressHUD.h"
- #import "GCDiscreetNotificationView.h"
- #import "NdUncaughtExceptionHandler.h"
- #import "JSNotifier.h"
- #import "AFOSCClient.h"
- #import "AFHTTPRequestOperation.h"
- #import "AFXMLRequestOperation.h"
还有一些宏定义都是比较常用方式的宏定义,比如定义的开源中国社区的api接口,这些接口变得当然很少了;
然后就剩下最后面的
- #ifdef DEBUG
- #define debugLog(...) NSLog(__VA_ARGS__)
- #define debugMethod() NSLog(@"%s", __func__)
- #else
- #define debugLog(...)
- #define debugMethod()
- #endif
工程有Debug Version和Release Version,Debug Version是程序开发过程中版本,它包含了所有调试信息,一些常用的NSLog打印日志,在程序调试过程工根据我们设置的调试信息可以看出什么地方出错,我们在运行运行一个小程序的时候,会不会首先就想到进行断点调试呢,应该是首先想着NSLog一下,看看哪个函数方法没执行,看看是不是哪个数组的值没取出来。Release Version是发布版本,不打印NSLog可以加快程序运行速度,减少内存使用。 但是到一个大工程中,会有很多很多这样的NSLog,在我们工程完美运行的时候,发布Release 版本的时候,难道我们去一行行的注释调NSLog吗?假如工程现在原来基础上发布一个version 1.2版本的,我们在修改程序的时候岂不是还把原来注释给取消,那就很麻烦很麻烦了。
所以,此处用到了宏指令
上段代码的意思就是 用宏指令做一个判断,如果DEBUG为真,则编译#ifdef到#endif宏定义,否则编译器就不编译;
这个DEBUG在哪设置呢,
在 "Target > Build Settings > Preprocessor Macros > Debug" 里有一个"DEBUG=1"。
现在我们来做一个测试:
取一个宏指令放到OSAppDelegate.m的application:didFinishLaunchingWithOptions:方法中,并用同一个NSLog做一个对比;
NSLog(@"%s", __func__);
debugMethod();
首先设置为Debug模式下,Product-->Edit Scheme
跳转到这个界面
当我设置Build Configuration成Debug时,打印效果图
当我设置Build Configuration成Release的,打印时效果图
当Run Test Profile Analyze Archive的时候,都可以根据需要设置Debug和Release两个模式运行;
所以我们完全可以用一个宏指令来设置是否打印调试信息;
龙神做个补充:
VC默认的预编译头文件就是StdAfx.h,因为头文件是不能编译的,所以我们还需要一个.CPP文件来作桥梁,VC默认的文件为StdAfx.cpp,这个文件里只有一句代码就是:#include "StdAfx.h".接下来要用它生成.PCH文件(预编译头文件),只不过这个文件是以二进制的形式产生的。如果如果丢了或删了.PCH文件只需要将程序再编译一下就有了。
要点:
-
预编译头文件一般扩展名为.PCH。
-
是把一个工程中较稳定的代码预先编译好放在一个文件(.PCH)里。
-
这些预先编译好的代码可以是任何的C/C++代码;
-
它们在整个工程中是较为稳定的,即在工程开发过程中不会经常被修改的代码。
-
有了pch可以加速后续的编译过程(有些代码不用重新编译了)。
相关文章
- [转] 前端实现文件的断点续传
- c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习
- PHP 文件操作
- my-small.ini、my-medium.ini、my-large.ini、my-huge.ini文件的作用
- ROS机器人程序设计(原书第2版)3.1.4 设置ROS节点core文件转储
- [WPF]为什么使用SaveFileDialog创建文件需要删除权限?
- 误删除vmdk文件恢复办法
- sysctl.conf文件的路径及作用
- base64编码文件下载
- 打包tfrecord文件,并读取
- git 无法提交空目录 ? (.gitkeep 文件的作用)
- 转 windows下安装pycharm并连接Linux的python环境 以及 windows 下notepad ++编辑 linux 的文件
- svn 修改文件的二进制或文本标记
- React脚手架工程化项目里ServiceWorker.js文件作用
- 使用QFileInfo类获取文件信息(在NTFS文件系统上,出于性能考虑,文件的所有权和权限检查在默认情况下是被禁用的,通过qt_ntfs_permission_lookup开启和操作。absolutePath()必须查询文件系统。而path()函数,可以直接作用于文件名本身,所以,path() 函数的运行会更快)
- 利用cwRsync客户端将Windows下文件同步到Linux
- shell流程控制(流程不可为空、if else流程、for循环、while循环/无限循环、until循环、分支结构case...esac、跳出循环)、shell 函数(定义需在开头、如何调用、有无return返回值差别、函数参数$1 / ${10}、$? 获取返回值及其限制)、shell 输入输出重定向、Here Document重定向方式、/dev/null文件作用、shell文件包含及使用
- 浅析npm报错ENOTFOUND npm ERR! network request to https://npm.***.com/*** failed 及 .npmrc 文件的作用、npm --verbose命令
- 浅析如何进行组件库入口文件设计及如何兼容全局引入和按需引入设计
- 审计文件的作用以及记录的内容,审计日志的记录,以及审计日志包括的内容
- Unity 工具 之 报错 Jenkins 执行/调用 Python 脚本,报错提示 ‘python‘ 不是内部或外部命令,也不是可运行的程序或批处理文件
- 嘿,虚拟SAN——欢迎这位新的虚拟文件存储兄弟
- Hadoop搭建,上传文件时出现错误,没有到主机的路由
- Node.js fs模块——读写文件