pch预编译头文件

版权所有,禁止匿名转载;禁止商业使用。

 -Prefix.pch结尾文件的文件,pch全称是“precompiled header”,也就是预编译头文件,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器编译速度我们知道当我们修改一个工程中某个文件代码时候,编译器并不是重新编译所有所有文件,而是编译改动过文件的,假如pch中某个文件修改了,那么pch整个文件里包含的的其他文件也会重新编译一次,这样就会消耗大量时间,所以它里面添加的文件最好是是很少变动或不变动的头文件或者是预编译的代码片段;

Pasted Graphic 2.tiff

Pasted Graphic 4.tiff

Precompile Prefix HeaderYES,那么pch会被预编译,预编译后的pch文件会被缓存起来,从而提高编译速度。当Precompile Prefix HeaderNO时,那么pch不会被预编译,而是在每一个用到它导入的框架类库的.m文件中编译一次。

#import <Availability.h>


#ifndef __IPHONE_4_0

#warning "This project uses features only available in iOS SDK 3.0 and later."

#endif


#ifdef __OBJC__

    #import <UIKit/UIKit.h>

    #import <Foundation/Foundation.h>

    #import "MCConst.h"

    #import "IQKeyboardManager.h"

    #import "MCCrouseDetailViewController.h"

#endif

再来看看我们开源中国iOS客户端pch文件


[cpp] view plain copy


  1. //  
  2. // Prefix header for all source files of the 'oschina' target in the 'oschina' project  
  3. //  
  4.   
  5. #import <Availability.h>  
  6.   
  7. #ifndef __IPHONE_4_0  
  8. #warning "This project uses features only available in iOS SDK 4.0 and later."  
  9. #endif  
  10.   
  11. #ifdef __OBJC__  
  12.     #import <UIKit/UIKit.h>  
  13.     #import <Foundation/Foundation.h>  
  14.     #import <CoreData/CoreData.h>  
  15.     #import <QuartzCore/QuartzCore.h>  
  16. //添加的预编译  
  17. #import "ASIHTTPRequest.h"  
  18. #import "ASIFormDataRequest.h"  
  19. #import "ASIHTTPRequestDelegate.h"  
  20. #import "ASIHTTPRequestConfig.h"  
  21. #import "TBXML.h"  
  22. #import "TBXML+HTTP.h"  
  23. #import "TBXML+Compression.h"  
  24. #import "Config.h"  
  25. #import "EGORefreshTableHeaderView.h"  
  26. #import "DataSingleton.h"  
  27. #import "ImgRecord.h"  
  28. #import "IconDownloader.h"  
  29. #import "MBProgressHUD.h"  
  30. #import "GCDiscreetNotificationView.h"  
  31. #import "NdUncaughtExceptionHandler.h"  
  32. #import "JSNotifier.h"  
  33. #import "AFOSCClient.h"  
  34. #import "AFHTTPRequestOperation.h"  
  35. #import "AFXMLRequestOperation.h"  
  36.   
  37. //api定义  
  38.   
  39. #define api_news_list @"http://www.oschina.net/action/api/news_list"  
  40. #define api_news_detail @"http://www.oschina.net/action/api/news_detail"  
  41. #define api_post_list @"http://www.oschina.net/action/api/post_list"  
  42. #define api_post_detail @"http://www.oschina.net/action/api/post_detail"  
  43. #define api_post_pub @"http://www.oschina.net/action/api/post_pub"  
  44. #define api_tweet_list @"http://www.oschina.net/action/api/tweet_list"  
  45. #define api_tweet_detail @"http://www.oschina.net/action/api/tweet_detail"  
  46. #define api_tweet_delete @"http://www.oschina.net/action/api/tweet_delete"  
  47. #define api_tweet_pub @"http://www.oschina.net/action/api/tweet_pub"  
  48. #define api_active_list @"http://www.oschina.net/action/api/active_list"  
  49. #define api_message_list @"http://www.oschina.net/action/api/message_list"  
  50. #define api_message_delete @"http://www.oschina.net/action/api/message_delete"  
  51. #define api_message_pub @"http://www.oschina.net/action/api/message_pub"  
  52. #define api_comment_list @"http://www.oschina.net/action/api/comment_list"  
  53. #define api_comment_pub @"http://www.oschina.net/action/api/comment_pub"  
  54. #define api_comment_reply @"http://www.oschina.net/action/api/comment_reply"  
  55. #define api_comment_delete @"http://www.oschina.net/action/api/comment_delete"  
  56. #define api_login_validate @"https://www.oschina.net/action/api/login_validate"  
  57. #define api_user_info @"http://www.oschina.net/action/api/user_info"  
  58. #define api_user_information @"http://www.oschina.net/action/api/user_information"  
  59. #define api_user_updaterelation @"http://www.oschina.net/action/api/user_updaterelation"  
  60. #define api_notice_clear @"http://www.oschina.net/action/api/notice_clear"  
  61. #define api_software_detail @"http://www.oschina.net/action/api/software_detail"  
  62. #define api_blog_detail @"http://www.oschina.net/action/api/blog_detail"  
  63. #define api_favorite_list @"http://www.oschina.net/action/api/favorite_list"  
  64. #define api_favorite_add @"http://www.oschina.net/action/api/favorite_add"  
  65. #define api_favorite_delete @"http://www.oschina.net/action/api/favorite_delete"  
  66. #define api_user_notice @"http://www.oschina.net/action/api/user_notice"  
  67. #define api_search_list @"http://www.oschina.net/action/api/search_list"  
  68. #define api_friends_list @"http://www.oschina.net/action/api/friends_list"  
  69. #define api_softwarecatalog_list @"http://www.oschina.net/action/api/softwarecatalog_list"  
  70. #define api_software_list @"http://www.oschina.net/action/api/software_list"  
  71. #define api_softwaretag_list @"http://www.oschina.net/action/api/softwaretag_list"  
  72. #define api_blogcomment_list @"http://www.oschina.net/action/api/blogcomment_list"  
  73. #define api_blogcomment_pub @"http://www.oschina.net/action/api/blogcomment_pub"  
  74. #define api_my_information @"http://www.oschina.net/action/api/my_information"  
  75. #define api_blogcomment_delete @"http://www.oschina.net/action/api/blogcomment_delete"  
  76. #define api_userblog_delete @"http://www.oschina.net/action/api/userblog_delete"  
  77. #define api_userblog_list @"http://www.oschina.net/action/api/userblog_list"  
  78. #define api_blog_list @"http://www.oschina.net/action/api/blog_list"  
  79. #define api_userinfo_update @"http://www.oschina.net/action/api/portrait_update"  
  80.   
  81. //宏定义 新闻  
  82. #define TweetCellIdentifier @"TweetCellIdentifier"  
  83. #define loadMoreIdentifier @"loadMoreIdentifier"  
  84. #define NewsCellIdentifier @"NewsCellIdentifier"  
  85. #define PostCellIdentifier @"PostCellIdentifier"  
  86. #define MsgCellIdentifier @"MsgCellIdentifier"  
  87. #define MsgUnitCellIdentifier @"MsgUnitCellIdentifier"  
  88. #define ActiveCellIdentifier @"ActiveCellIdentifier"  
  89. #define UserActiveCellIdentifier @"UserActiveCellIdentifier"  
  90. #define ColorActiveCellIdentifier @"ColorActiveCellIdentifier"  
  91. #define RTActiveCellIdentifier @"RTActiveCellIdentifier"  
  92. #define ColorUserActiveCellIdentifier @"ColorUserActiveCellIdentifier"  
  93. #define ProfielCellIdentifier @"ProfielCellIdentifier"  
  94. #define CommentCellIdentifier @"CommentCellIdentifier"  
  95. #define NormalCellIdentifier @"NormalCellIdentifier"  
  96. #define FavoriteCellIdentifier @"FavoriteCellIdentifier"  
  97. #define FriendCellIdentifier @"FriendCellIdentifier"  
  98. #define SoftwareCellIdentifier @"SoftwareCellIdentifier"  
  99. #define SoftwareCatalogIdentifier @"SoftwareCatalogIdentifier"  
  100. #define SettingTableIdentifier @"SettingTableIdentifier"  
  101. #define MyInfoCellIdentifier @"MyInfoCellIdentifier"  
  102. #define MyPortraitCellIdentifier @"MyPortraitCellIdentifier"  
  103.   
  104. #define loadNext20Tip @"下面 20  . . ."  
  105. #define loadingTip @"正在加载 . . ."  
  106. #define networkError @"网络无连接"  
  107. #define noNetworkTip @"网络无连接"  
  108.   
  109. //消息通知固定字符串  
  110. #define Notification_DetailCommentCount @"Notification_DetailCommentCount"  
  111. #define Notification_NoticeUpdate @"Notification_NoticeUpdate"  
  112. #define Notification_TabClick @"Notification_TabClick"  
  113.   
  114. //html头部  
  115. #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>"  
  116. #define HTML_Bottom @"<div style='margin-bottom:60px'/>"  
  117.   
  118. #define USERAGENT @"OSChina.NET/iOS/5.0"  
  119.   
  120. #define AppVersion @"1.6.1"  
  121.   
  122. #ifdef DEBUG  
  123. #define debugLog(...) NSLog(__VA_ARGS__)  
  124. #define debugMethod() NSLog(@"%s", __func__)  
  125. #else  
  126. #define debugLog(...)  
  127. #define debugMethod()  
  128. #endif  
  129.   
  130. #endif  
  131. 其实,这些文件特殊之处在于他们都是第三方类库的头文件,第三方类库将一些对象进行高度封装,留下接口,然后我们根据类库接口直接调用就可以,这些第三方类库一般都比iOS原生自带的更加简单易用,比如TBXML解析库,比iOS自带的NSXMLPaser解析器速度功能上都会好一些;
  132. 还有一些宏定义都是比较常用方式的宏定义,比如定义的开源中国社区的api接口,这些接口变得当然很少了;
  133. [cpp] view plain copy
  134. #ifdef DEBUG  
  135. #define debugLog(...) NSLog(__VA_ARGS__)  
  136. #define debugMethod() NSLog(@"%s", __func__)  
  137. #else  
  138. #define debugLog(...)  
  139. #define debugMethod()  
  140. #endif  
  141.  工程有Debug VersionRelease VersionDebug Version是程序开发过程中版本,它包含了所有调试信息,一些常用的NSLog打印日志,在程序调试过程工根据我们设置的调试信息可以看出什么地方出错,我们在运行运行一个小程序的时候,会不会首先就想到进行断点调试呢,应该是首先想着NSLog一下,看看哪个函数方法没执行,看看是不是哪个数组的值没取出来。Release Version是发布版本,不打印NSLog可以加快程序运行速度,减少内存使用。   但是到一个大工程中,会有很多很多这样的NSLog,在我们工程完美运行的时候,发布Release 版本的时候,难道我们去一行行的注释调NSLog吗?假如工程现在原来基础上发布一个version 1.2版本的,我们在修改程序的时候岂不是还把原来注释给取消,那就很麻烦很麻烦了。
  142. 所以,此处用到了宏指令
  143. 上段代码的意思就是 用宏指令做一个判断,如果DEBUG为真,则编译#ifdef#endif宏定义,否则编译器就不编译;
  144. 这个DEBUG在哪设置呢,
  145. "Target > Build Settings > Preprocessor Macros > Debug" 里有一个"DEBUG=1"
    1365294947_8161.png
  146. 现在我们来做一个测试:
  147. 取一个宏指令放到OSAppDelegate.mapplication:didFinishLaunchingWithOptions:方法中,并用同一个NSLog做一个对比;
  148. NSLog(@"%s", __func__);
  149.  debugMethod();
  150. 首先设置为Debug模式下,Product-->Edit Scheme

Pasted Graphic.tiff

Pasted Graphic 1.tiff


Pasted Graphic 5.tiff

debug版本有信息打出,而release版本没有信息打出

0 0