• ios开发之.pch文件的使用


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


    在新建一个工程时,pch后缀文件里代码是

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

    或许你会觉得这预编译代码很少,但是你可以查看一下UIKit.h的定义文件中


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

    这些不少了吧,工程每次运行都编译是不是很费时间,这些是苹果公司内部定义的标准头文件,我们不能也没有权限修改这些头文件定义内容,所以,当放到pch文件中会加速编译过程;



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

    1. //
    2. // Prefix header for all source files of the 'oschina' target in the 'oschina' project
    3. //
    4. #import <Availability.h>
    5. #ifndef __IPHONE_4_0
    6. #warning "This project uses features only available in iOS SDK 4.0 and later."
    7. #endif
    8. #ifdef __OBJC__
    9.     #import <UIKit/UIKit.h>
    10.     #import <Foundation/Foundation.h>
    11.     #import <CoreData/CoreData.h>
    12.     #import <QuartzCore/QuartzCore.h>
    13. //添加的预编译
    14. #import "ASIHTTPRequest.h"
    15. #import "ASIFormDataRequest.h"
    16. #import "ASIHTTPRequestDelegate.h"
    17. #import "ASIHTTPRequestConfig.h"
    18. #import "TBXML.h"
    19. #import "TBXML+HTTP.h"
    20. #import "TBXML+Compression.h"
    21. #import "Config.h"
    22. #import "EGORefreshTableHeaderView.h"
    23. #import "DataSingleton.h"
    24. #import "ImgRecord.h"
    25. #import "IconDownloader.h"
    26. #import "MBProgressHUD.h"
    27. #import "GCDiscreetNotificationView.h"
    28. #import "NdUncaughtExceptionHandler.h"
    29. #import "JSNotifier.h"
    30. #import "AFOSCClient.h"
    31. #import "AFHTTPRequestOperation.h"
    32. #import "AFXMLRequestOperation.h"
    33. //api定义
    34. #define api_news_list @"http://www.oschina.net/action/api/news_list"
    35. #define api_news_detail @"http://www.oschina.net/action/api/news_detail"
    36. #define api_post_list @"http://www.oschina.net/action/api/post_list"
    37. #define api_post_detail @"http://www.oschina.net/action/api/post_detail"
    38. #define api_post_pub @"http://www.oschina.net/action/api/post_pub"
    39. #define api_tweet_list @"http://www.oschina.net/action/api/tweet_list"
    40. #define api_tweet_detail @"http://www.oschina.net/action/api/tweet_detail"
    41. #define api_tweet_delete @"http://www.oschina.net/action/api/tweet_delete"
    42. #define api_tweet_pub @"http://www.oschina.net/action/api/tweet_pub"
    43. #define api_active_list @"http://www.oschina.net/action/api/active_list"
    44. #define api_message_list @"http://www.oschina.net/action/api/message_list"
    45. #define api_message_delete @"http://www.oschina.net/action/api/message_delete"
    46. #define api_message_pub @"http://www.oschina.net/action/api/message_pub"
    47. #define api_comment_list @"http://www.oschina.net/action/api/comment_list"
    48. #define api_comment_pub @"http://www.oschina.net/action/api/comment_pub"
    49. #define api_comment_reply @"http://www.oschina.net/action/api/comment_reply"
    50. #define api_comment_delete @"http://www.oschina.net/action/api/comment_delete"
    51. #define api_login_validate @"https://www.oschina.net/action/api/login_validate"
    52. #define api_user_info @"http://www.oschina.net/action/api/user_info"
    53. #define api_user_information @"http://www.oschina.net/action/api/user_information"
    54. #define api_user_updaterelation @"http://www.oschina.net/action/api/user_updaterelation"
    55. #define api_notice_clear @"http://www.oschina.net/action/api/notice_clear"
    56. #define api_software_detail @"http://www.oschina.net/action/api/software_detail"
    57. #define api_blog_detail @"http://www.oschina.net/action/api/blog_detail"
    58. #define api_favorite_list @"http://www.oschina.net/action/api/favorite_list"
    59. #define api_favorite_add @"http://www.oschina.net/action/api/favorite_add"
    60. #define api_favorite_delete @"http://www.oschina.net/action/api/favorite_delete"
    61. #define api_user_notice @"http://www.oschina.net/action/api/user_notice"
    62. #define api_search_list @"http://www.oschina.net/action/api/search_list"
    63. #define api_friends_list @"http://www.oschina.net/action/api/friends_list"
    64. #define api_softwarecatalog_list @"http://www.oschina.net/action/api/softwarecatalog_list"
    65. #define api_software_list @"http://www.oschina.net/action/api/software_list"
    66. #define api_softwaretag_list @"http://www.oschina.net/action/api/softwaretag_list"
    67. #define api_blogcomment_list @"http://www.oschina.net/action/api/blogcomment_list"
    68. #define api_blogcomment_pub @"http://www.oschina.net/action/api/blogcomment_pub"
    69. #define api_my_information @"http://www.oschina.net/action/api/my_information"
    70. #define api_blogcomment_delete @"http://www.oschina.net/action/api/blogcomment_delete"
    71. #define api_userblog_delete @"http://www.oschina.net/action/api/userblog_delete"
    72. #define api_userblog_list @"http://www.oschina.net/action/api/userblog_list"
    73. #define api_blog_list @"http://www.oschina.net/action/api/blog_list"
    74. #define api_userinfo_update @"http://www.oschina.net/action/api/portrait_update"
    75. //宏定义 新闻
    76. #define TweetCellIdentifier @"TweetCellIdentifier"
    77. #define loadMoreIdentifier @"loadMoreIdentifier"
    78. #define NewsCellIdentifier @"NewsCellIdentifier"
    79. #define PostCellIdentifier @"PostCellIdentifier"
    80. #define MsgCellIdentifier @"MsgCellIdentifier"
    81. #define MsgUnitCellIdentifier @"MsgUnitCellIdentifier"
    82. #define ActiveCellIdentifier @"ActiveCellIdentifier"
    83. #define UserActiveCellIdentifier @"UserActiveCellIdentifier"
    84. #define ColorActiveCellIdentifier @"ColorActiveCellIdentifier"
    85. #define RTActiveCellIdentifier @"RTActiveCellIdentifier"
    86. #define ColorUserActiveCellIdentifier @"ColorUserActiveCellIdentifier"
    87. #define ProfielCellIdentifier @"ProfielCellIdentifier"
    88. #define CommentCellIdentifier @"CommentCellIdentifier"
    89. #define NormalCellIdentifier @"NormalCellIdentifier"
    90. #define FavoriteCellIdentifier @"FavoriteCellIdentifier"
    91. #define FriendCellIdentifier @"FriendCellIdentifier"
    92. #define SoftwareCellIdentifier @"SoftwareCellIdentifier"
    93. #define SoftwareCatalogIdentifier @"SoftwareCatalogIdentifier"
    94. #define SettingTableIdentifier @"SettingTableIdentifier"
    95. #define MyInfoCellIdentifier @"MyInfoCellIdentifier"
    96. #define MyPortraitCellIdentifier @"MyPortraitCellIdentifier"
    97. #define loadNext20Tip @"下面 20 项 . . ."
    98. #define loadingTip @"正在加载 . . ."
    99. #define networkError @"网络无连接"
    100. #define noNetworkTip @"网络无连接"
    101. //消息通知固定字符串
    102. #define Notification_DetailCommentCount @"Notification_DetailCommentCount"
    103. #define Notification_NoticeUpdate @"Notification_NoticeUpdate"
    104. #define Notification_TabClick @"Notification_TabClick"
    105. //html头部
    106. #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- 300px;}#oschina_body {font-size:16px;max-300px;line-height:24px;} #oschina_body table{max-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>"
    107. #define HTML_Bottom @"<div style='margin-bottom:60px'/>"
    108. #define USERAGENT @"OSChina.NET/iOS/5.0"
    109. #define AppVersion @"1.6.1"
    110. #ifdef DEBUG
    111. #define debugLog(...) NSLog(__VA_ARGS__)
    112. #define debugMethod() NSLog(@"%s", __func__)
    113. #else
    114. #define debugLog(...)
    115. #define debugMethod()
    116. #endif
    117. #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接口,这些接口变得当然很少了;


    然后就剩下最后面的

    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宏定义,否则编译器就不编译;
    这个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两个模式运行;
    所以我们完全可以用一个宏指令来设置是否打印调试信息;

  • 相关阅读:
    while,dowhile,for循环和forin
    position:fixed;支持ie6,清除e6下抖动。
    数组concat()和slice()方法
    函数内arguments.callee的用法
    [转]十天学习PHP之第六天(PHP)学会添加删除修改数据 (20091125 14:26)
    [转]十天学习PHP之第三天(PHP)学会构建数据库
    [转]十天学习PHP之第二天(PHP)掌握php的流程控制
    php集成软件VertrigoServ(PHP安装)
    [转]十天学习PHP之第一天(PHP)基础知识
    PHP入门
  • 原文地址:https://www.cnblogs.com/xubojoy/p/3934653.html
Copyright © 2020-2023  润新知