关于<AppName>-Prefix.pch文件的科幻用法

本文深入探讨了Objective-C开发中Prefix.pch文件的使用技巧,通过定义变量、导入库和简化代码引用,提高开发效率。具体展示了如何减少冗余引用、简化函数调用和统一库导入,以实现代码的简洁性和易维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch。对于这个文件,很长一段时间里笔者都没觉得它碍事。直到有一天笔者学习NSLog看网上的教程,大家是怎样在最终提交应用的时候,一次性将NSLog语句移除。
网上大多转来转去的方法,都是说把如下的语句

#ifdef DEBUG
#    define DLog(...) NSLog(__VA_ARGS__)
#else
#    define DLog(...) /* */
#endif
#define ALog(...) NSLog(__VA_ARGS__)


加到 <AppName>-Prefix.pch的文件中。虽然对于这种作法,笔者最终由于,不想在调试一个东西而出现一堆东西,最终没有使用这种方法。但是 <AppName>-Prefix.pch这个文件,最终引起了作者的注意。
网上查了一下有解释说.pch是“precompiled header”的意思,那么字面意思理解就是预编译文件头喽。据说在程序编译前都优先编译好这里指定的文件,这样可以加快编译速度。好吧,我们来看看默认这个文件里包含什么:

//
// Prefix header for all source files of the 'HelloWorld' target in the 'HelloWorld' 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>
#endif

按着Command键,再点开UIKit/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>

举个例子,有没有注意到#import <UIKit/UILabel.h>?笔者在使用如下语句的时候:

UILabel *_testLabel = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 15)];

曾经不止一次的怀疑,这个UILabel是哪来的,为嘛可以直接用。这个文件就说明了一切!
对此你有什么想法?我的想法就是:如果我每个View几乎都要用到ASIHTTPRequest的话,那么我只在这里引用一次ASIHTTPRequest.h就够了!
这样我就可以在需要使用的ASIHTTPRequest的时候直接用了!
于是我工程里的这个文件变成了这样:

//
// Prefix header for all source files of the 'HelloWorld' target in the 'HelloWorld' project
//

#import <Availability.h>

#ifndef __IPHONE_3_0
#warning "This project uses features only available in iPhone SDK 3.0 and later."
#endif

#ifdef __OBJC__
  #import <UIKit/UIKit.h>
  #import <Foundation/Foundation.h>
  #import "nstimer.h"
  #import "ButtWithBlockActions.h"
  #import "TKAlertCenter.h"
  #define TKDCenter [TKAlertCenter defaultCenter] 
  #import "DataManager.h"
  #define DDManager [DataManager defaultManager]
  #define DataMemDic [DDManager memDic]
  //for checkUpdateVersion
  #import "Reachability.h"
  #import "ASIHTTPRequest.h"
  #import "ASIFormDataRequest.h"
  #import "JSON.h"
  
  //BlockAlert
  #import "RIButtonItem.h"
  #import "UIAlertView+Blocks.h"
  #import "UIActionSheet+Blocks.h"

  #import <QuartzCore/QuartzCore.h>
  #import "function.h"
  #import "MediaPlayer/MediaPlayer.h"
#endif

这样的话,我觉得的TKAlertCenter神马的,用起来顺手多了,再也不用每次使用的时候先引用一下了。
注意到上面的

  #import "TKAlertCenter.h"
  #define TKDCenter [TKAlertCenter defaultCenter]

没?我觉得每次都写:

[[TKAlertCenter defaultCenter] postAlertWithMessage:@"http://blog.cnrainbird.com"];

这样的句子太长了!有了上面的定义,我现在变成这样写了:

  [TKDCenter postAlertWithMessage:@"http://blog.cnrainbird.com"];

是不是短多了?
同样了,我的每个工程的-Prefix.pch文件里还定义了如下的变量:

#define NDSUD [NSUserDefaults standardUserDefaults]
#define NNCDC [NSNotificationCenter defaultCenter]
#define FM [NSFileManager defaultManager]
#define APPSHAREAPP [UIApplication sharedApplication]
#define appOrientation [[UIApplication sharedApplication] statusBarOrientation]
#define DocumentPath [NSString stringWithFormat:@"%@/Library/Caches",NSHomeDirectory()]
#define IOSSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]

这样做有两个好处:
1.变量名变短了。
虽然object-c里强调变量要完整的表达意思,但是类似[NSUserDefaults standardUserDefaults]这样的单例实在太长了,参数名再长点儿一行都写不下了,很不易于阅读。
2.方便修改
看到DocumentPath这个变量没?大家伙还都记得的IOS5刚出来那会儿,N多app因为把存放文件的路径搁到了Documents下App升级被拒的事儿吧?当时因为我已经用上了这个变量,直接在这个一改,嘿嘿,几十个文件立马跟着变了,省力更省心!

最后发张图,证明我的存在:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssm</display-name> <!-- 编码过滤器开始 --> <filter> <filter-name>charset</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charset</filter-name> <!-- <url-pattern>*.action</url-pattern> --> <servlet-name>springmvc</servlet-name> </filter-mapping> <!-- 编码过滤器结束 --> <!-- 配置spring容器的监听器 开始 目地:在启用tomcat的时候加载 applicationContext.xml ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml') --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置上下文的参数 contextConfigLocation 在org.springframework.web.context.ContextLoader里面--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置spring容器的监听器 结束--> <!-- 配置前端控制器开始 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置contextConfigLocation --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- load-on-startup 1 代表当tomcat启动加web.xml里就创建 DispatcherServlet的对象 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- *.action:所以的在xxxx.action结尾的请求都交给DispatcherServlet /* 所以的请求都交给DispacherServlet 包含静态文件的地址 js css png gif,使用此种方式可以实现 RESTful风格的url --> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- 配置前端控制器结束 --> <!--欢迎页面 当tomcat启动的时候 直接执行的页面--> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> classpath填什么?
最新发布
08-08
在Spring项目中,`classpath` 是一种用于定位资源文件(如配置文件、静态资源等)的路径写法。它通常用于指定资源位于类路径下,即编译后的 `resources` 目录或其子目录。以下是 `classpath` 配置的一些常见写法和使用场景。 ### classpath 配置的写法 1. **直接引用根目录下的资源** ```properties spring.config.location=classpath:application.properties ``` 这种写法表示从类路径的根目录下加载 `application.properties` 文件[^1]。 2. **引用子目录中的资源** ```properties spring.config.location=classpath:config/application.properties ``` 此配置表示从类路径下的 `config` 子目录中加载 `application.properties` 文件[^2]。 3. **指定多个配置路径** 如果需要从多个路径加载配置文件,可以使用逗号 `,` 分隔多个路径: ```properties spring.config.location=classpath:config/,classpath:external/ ``` 这种方式会从 `config` 和 `external` 两个目录中加载配置文件[^2]。 4. **结合 spring.config.name 使用** 可以通过 `spring.config.name` 指定配置文件的名称,再结合 `classpath` 使用: ```properties spring.config.name=myapp spring.config.location=classpath:config/ ``` 此配置会从类路径的 `config` 目录下加载名为 `myapp.properties` 的文件[^1]。 5. **YAML 配置示例** 如果使用 `.yml` 或 `.yaml` 格式的配置文件,也可以在 `application.yml` 中设置 `classpath` 相关内容: ```yaml spring: config: location: classpath:config/ name: myapp ``` 上述配置与上面的 `.properties` 写法功能一致。 ### 注意事项 - `classpath` 的查找范围包括 `resources` 目录及其子目录。 - 如果需要将配置文件放在项目根目录之外的位置(如外部文件系统),可以使用 `file:` 前缀来指定绝对路径。 - 在实际开发中,建议统一使用一种配置文件格式(如 `.properties` 或 `.yml`),以降低维护成本。 ### 示例代码 以下是一个通过 `classpath` 加载配置文件的简单示例: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:config/app.properties") public class AppConfig { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; // Getter and Setter } ``` 在 `app.properties` 文件中可以定义如下内容: ```properties app.name=MyApplication app.version=1.0.0 ``` 上述代码通过 `@PropertySource` 注解从 `classpath` 中加载 `app.properties` 文件,并通过 `@Value` 注解读取配置项[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值