開源中國iOS用戶端學習——(一)Prefix.pch檔案

來源:互聯網
上載者:User

         當我們建立一個工程的時候,在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"

其實,這些檔案特殊之處在於他們都是第三方類庫的標頭檔,第三方類庫將一些對象進行高度封裝,留下介面,然後我們根據類庫介面直接調用就可以,這些第三方類庫一般都比iOS原生內建的更加簡單易用,比如TBXML解析庫,比iOS內建的NSXMLPaser解析器速度功能上都會好一些;


還有一些宏定義都是比較常用方式的宏定義,比如定義的開源中國社區的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兩個模式運行;

所以我們完全可以用一個巨集指令來設定是否列印調試資訊;




歡迎轉載分享,請註明出處http://blog.csdn.net/duxinfeng2010

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.