開源中國iOS客戶端學習——(一)Prefix.pch文件

 當咱們新建一個工程的時候,在Supporting FIles文件下會看到一個以  -Prefix.pch結尾文件的文件,pch全稱是「precompiled header」,也就是預編譯頭文件,該文件裏存放的工程中一些不常被修改的代碼,好比經常使用的框架頭文件,這樣作的目的提升編譯器編譯速度。咱們知道當咱們修改一個工程中某個文件代碼時候,編譯器並非從新編譯全部全部文件,而是編譯改動過文件的,假如pch中某個文件修改了,那麼pch整個文件裏包含的的其餘文件也會從新編譯一次,這樣就會消耗大量時間,因此它裏面添加的文件最好是是不多變更或不變更的頭文件或者是預編譯的代碼片斷; html


在新建一個工程時,pch後綴文件裏代碼是 小程序

  1. #import <Availability.h>  
  2.   
  3. #ifndef __IPHONE_4_0  
  4. #warning "This project uses features only available in iOS SDK 4.0 and later."  
  5. #endif  
  6.   
  7. #ifdef __OBJC__  
  8.     #import <UIKit/UIKit.h>  
  9.     #import <Foundation/Foundation.h>  
  10. #endif  

或許你會以爲這預編譯代碼不多,可是你能夠查看一下UIKit.h的定義文件中

  1. //  
  2. //  UIKit.h  
  3. //  UIKit  
  4. //  
  5. //  Copyright (c) 2005-2011, Apple Inc. All rights reserved.  
  6. //  
  7.   
  8. #import <UIKit/UIKitDefines.h>  
  9. #import <UIKit/UIAccelerometer.h>  
  10. #import <UIKit/UIAccessibility.h>   
  11. #import <UIKit/UIActivityIndicatorView.h>  
  12. #import <UIKit/UIAlert.h>  
  13. #import <UIKit/UIApplication.h>  
  14. #import <UIKit/UIBarButtonItem.h>  
  15. #import <UIKit/UIBarItem.h>  
  16. #import <UIKit/UIBezierPath.h>  
  17. #import <UIKit/UIButton.h>  
  18. #import <UIKit/UIColor.h>  
  19. #import <UIKit/UIControl.h>  
  20. #import <UIKit/UIDataDetectors.h>  
  21. #import <UIKit/UIDatePicker.h>  
  22. #import <UIKit/UIDevice.h>  
  23. #import <UIKit/UIDocument.h>  
  24. #import <UIKit/UIDocumentInteractionController.h>  
  25. #import <UIKit/UIEvent.h>  
  26. #import <UIKit/UIFont.h>  
  27. #import <UIKit/UIGeometry.h>  
  28. #import <UIKit/UIGestureRecognizer.h>  
  29. #import <UIKit/UIGraphics.h>  
  30. #import <UIKit/UIImage.h>  
  31. #import <UIKit/UIImagePickerController.h>  
  32. #import <UIKit/UIImageView.h>  
  33. #import <UIKit/UIInterface.h>  
  34. #import <UIKit/UILabel.h>  
  35. #import <UIKit/UILocalNotification.h>  
  36. #import <UIKit/UILocalizedIndexedCollation.h>  
  37. #import <UIKit/UILongPressGestureRecognizer.h>  
  38. #import <UIKit/UIManagedDocument.h>  
  39. #import <UIKit/UIMenuController.h>  
  40. #import <UIKit/UINavigationBar.h>  
  41. #import <UIKit/UINavigationController.h>  
  42. #import <UIKit/UINib.h>  
  43. #import <UIKit/UINibDeclarations.h>  
  44. #import <UIKit/UINibLoading.h>  
  45. #import <UIKit/UIPageControl.h>  
  46. #import <UIKit/UIPageViewController.h>  
  47. #import <UIKit/UIPanGestureRecognizer.h>  
  48. #import <UIKit/UIPasteboard.h>  
  49. #import <UIKit/UIPickerView.h>  
  50. #import <UIKit/UIPinchGestureRecognizer.h>  
  51. #import <UIKit/UIPopoverController.h>  
  52. #import <UIKit/UIPopoverBackgroundView.h>  
  53. #import <UIKit/UIPrintError.h>  
  54. #import <UIKit/UIPrintFormatter.h>  
  55. #import <UIKit/UIPrintInfo.h>  
  56. #import <UIKit/UIPrintInteractionController.h>  
  57. #import <UIKit/UIPrintPageRenderer.h>  
  58. #import <UIKit/UIPrintPaper.h>  
  59. #import <UIKit/UIProgressView.h>  
  60. #import <UIKit/UIReferenceLibraryViewController.h>  
  61. #import <UIKit/UIResponder.h>  
  62. #import <UIKit/UIRotationGestureRecognizer.h>  
  63. #import <UIKit/UIScreen.h>  
  64. #import <UIKit/UIScreenMode.h>  
  65. #import <UIKit/UIScrollView.h>  
  66. #import <UIKit/UISearchBar.h>  
  67. #import <UIKit/UISearchDisplayController.h>  
  68. #import <UIKit/UISegmentedControl.h>  
  69. #import <UIKit/UISlider.h>  
  70. #import <UIKit/UISplitViewController.h>  
  71. #import <UIKit/UIStepper.h>  
  72. #import <UIKit/UIStoryboard.h>  
  73. #import <UIKit/UIStoryboardPopoverSegue.h>  
  74. #import <UIKit/UIStoryboardSegue.h>  
  75. #import <UIKit/UIStringDrawing.h>  
  76. #import <UIKit/UISwipeGestureRecognizer.h>  
  77. #import <UIKit/UISwitch.h>  
  78. #import <UIKit/UITabBar.h>  
  79. #import <UIKit/UITabBarController.h>  
  80. #import <UIKit/UITabBarItem.h>  
  81. #import <UIKit/UITableView.h>  
  82. #import <UIKit/UITableViewCell.h>  
  83. #import <UIKit/UITableViewController.h>  
  84. #import <UIKit/UITapGestureRecognizer.h>  
  85. #import <UIKit/UITextField.h>  
  86. #import <UIKit/UITextInput.h>  
  87. #import <UIKit/UITextInputTraits.h>  
  88. #import <UIKit/UITextView.h>  
  89. #import <UIKit/UIToolbar.h>  
  90. #import <UIKit/UITouch.h>  
  91. #import <UIKit/UIVideoEditorController.h>  
  92. #import <UIKit/UIView.h>  
  93. #import <UIKit/UIViewController.h>  
  94. #import <UIKit/UIWebView.h>  
  95. #import <UIKit/UIWindow.h>  

這些很多了吧,工程每次運行都編譯是否是很費時間,這些是蘋果公司內部定義的標準頭文件,咱們不能也沒有權限修改這些頭文件定義內容,因此,當放到pch文件中會加速編譯過程;


再來看看咱們開源中國iOS客戶端pch文件 api

  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  


咱們看到有這樣些文件也被添加到裏面,可能會想難道這些頭文件變化不大嗎? 數組

  1. //添加的預編譯  
  2. #import "ASIHTTPRequest.h"  
  3. #import "ASIFormDataRequest.h"  
  4. #import "ASIHTTPRequestDelegate.h"  
  5. #import "ASIHTTPRequestConfig.h"  
  6. #import "TBXML.h"  
  7. #import "TBXML+HTTP.h"  
  8. #import "TBXML+Compression.h"  
  9. #import "Config.h"  
  10. #import "EGORefreshTableHeaderView.h"  
  11. #import "DataSingleton.h"  
  12. #import "ImgRecord.h"  
  13. #import "IconDownloader.h"  
  14. #import "MBProgressHUD.h"  
  15. #import "GCDiscreetNotificationView.h"  
  16. #import "NdUncaughtExceptionHandler.h"  
  17. #import "JSNotifier.h"  
  18. #import "AFOSCClient.h"  
  19. #import "AFHTTPRequestOperation.h"  
  20. #import "AFXMLRequestOperation.h"  
其實,這些文件特殊之處在於他們都是第三方類庫的頭文件,第三方類庫將一些對象進行高度封裝,留下接口,而後咱們根據類庫接口直接調用就能夠,這些第三方類庫通常都比iOS原生自帶的更加簡單易用,好比TBXML解析庫,比iOS自帶的NSXMLPaser解析器速度功能上都會好一些;


還有一些宏定義都是比較經常使用方式的宏定義,好比定義的開源中國社區的api接口,這些接口變得固然不多了; 網絡


而後就剩下最後面的 app

  1. #ifdef DEBUG  
  2. #define debugLog(...) NSLog(__VA_ARGS__)  
  3. #define debugMethod() NSLog(@"%s", __func__)  
  4. #else  
  5. #define debugLog(...)  
  6. #define debugMethod()  
  7. #endif  

        工程有Debug Version和Release Version,Debug Version是程序開發過程當中版本,它包含了全部調試信息,一些經常使用的NSLog打印日誌,在程序調試過程工根據咱們設置的調試信息能夠看出什麼地方出錯,咱們在運行運行一個小程序的時候,會不會首先就想到進行斷點調試呢,應該是首先想着NSLog一下,看看哪一個函數方法沒執行,看看是否是哪一個數組的值沒取出來。Release Version是發佈版本,不打印NSLog能夠加快程序運行速度,減小內存使用。   可是到一個大工程中,會有不少不少這樣的NSLog,在咱們工程完美運行的時候,發佈Release 版本的時候,難道咱們去一行行的註釋調NSLog嗎?假如工程如今原來基礎上發佈一個version 1.2版本的,咱們在修改程序的時候豈不是還把原來註釋給取消,那就很麻煩很麻煩了。

因此,此處用到了宏指令 框架


上段代碼的意思就是 用宏指令作一個判斷,若是DEBUG爲真,則編譯#ifdef到#endif宏定義,不然編譯器就不編譯; ide

這個DEBUG在哪設置呢, 函數

在 "Target > Build Settings > Preprocessor Macros > Debug" 裏有一個"DEBUG=1"。

post


如今咱們來作一個測試:

取一個宏指令放到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兩個模式運行;

因此咱們徹底能夠用一個宏指令來設置是否打印調試信息;

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息