Case: Open source China iOS client When we create a new project, under the supporting files file will see a file ending with-prefix.pch, PCH full name is "precompiled Header", that is, the precompiled header file, the file stored in the project Some of the less frequently modified code, such as a commonly used framework header file, is intended to improve compiler compilation speed. We know that when we modify a file code in a project, the compiler does not recompile all the files, but compiles the changed files, if a file in the PCH is modified, then the other files contained in the PCH file will be recompiled once, which will consume a lot of time. Therefore it is better to add the file inside the header file that is rarely changed or not changed, or is a precompiled code fragment;
When you create a new project, the code in the PCH suffix file is
- #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
Copy Code
You might think this precompiled code is rare, but you can look at the UIKit.h definition file
- //
- 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>
Copy Code
This is a lot of it, the project every time the compilation is not very time-consuming, these are Apple's internal definition of standard header files, we can not and do not have permission to modify these header file definition content, so, when placed in the PCH file will speed up the compilation process;
Take a look at our open source China iOS client PCH file
- //
- 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>
- Pre-compilation added
- #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 definition
- #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"
- Macro Definition News
- #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 @ "below 20 items ..."
- #define LOADINGTIP @ "Loading ..."
- #define NETWORKERROR @ "Network no Connection"
- #define NONETWORKTIP @ "Network no Connection"
- Message Notification fixed string
- #define NOTIFICATION_DETAILCOMMENTCOUNT @ "Notification_detailcommentcount"
- #define Notification_noticeupdate @ "Notification_noticeupdate"
- #define Notification_tabclick @ "Notification_tabclick"
- HTML header
- #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;p adding: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
Copy Code
We see that some of these files have also been added to the inside, and might wonder if these headers have changed much?
- Pre-compilation added
- #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"
Copy Code
In fact, these files are special in that they are the third-party class library header files, the third-party class library to highly encapsulate some objects, leaving the interface, and then we can directly call on the class library interface, these third-party libraries are generally more simple than the original native iOS, such as the Tbxml parsing library, It's better than the Nsxmlpaser parser speed function that comes with iOS.
There are also macro definitions that are more commonly used, such as the API interfaces of the defined open source Chinese community, which are, of course, rare;
And then the last thing left.
- #ifdef DEBUG
- #define DEBUGLOG (...) NSLog (__va_args__)
- #define DEBUGMETHOD () NSLog (@ "%s", __func__)
- #else
- #define DEBUGLOG (...)
- #define DEBUGMETHOD ()
- #endif
Copy Code
The project has debug version and release Version,debug versions is the program development process, it contains all the debugging information, some commonly used nslog print logs, in the program debugging process according to the debugging information we set can see what place error, We run a small program when running, will not be the first to think of debugging a breakpoint, it should be the first thought of NSLog, see which function method did not execute, to see if the value of which array is not taken out. Release version is a release, and not printing nslog can speed up the program and reduce memory usage. But to a big project, there will be a lot of such nslog, when we work perfectly run, release release version of the time, we go to a line of comments NSLog? If the project is now based on the original version 1.2 release, we modify the program when the original comment is not also canceled, it is very troublesome and troublesome.
So, here's the macro directive.
the above code means to make a judgment with the macro command, if the debug is true, then compile #ifdef to #endif macro definition, otherwise the compiler will not compile;
where does this debug set up,
there is a "debug=1" in "Target > Build Settings > Preprocessor Macros > Debug".
now let's do a test:
take a macro and put it in the OSAPPDELEGATE.M.Application:didfinishlaunchingwithoptions: In the method, make a contrast with the same nslog;
NSLog (@ "%s", __func__);
Debugmethod ();
first set to debug mode, Product-->edit scheme
Jump to this interface
When I set the build configuration to debug, the print
when I set the build configuration to release, when printing
when run Test Profile Analyze archive, the debug and release two modes can be set as required.
so we can use a macro command to set whether or not to print debugging information;
Use of the. pch file developed by iOS