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

本文探讨了 iOS 开发中 Prefix.pch 文件的作用及其优化方案,通过在此文件中引入常用的头文件,简化代码并提高开发效率。

转载请注明: 转自Rainbird的个人博客
   本文链接: 关于<AppName>-Prefix.pch文件的科幻用法

我们知道,每新建立一个工程,比如说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"?> 4.0.0 <groupId>org.example</groupId> <artifactId>scala_tutorial</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scala.version>2.12.11</scala.version> <spark.version>3.1.1</spark.version> <hadoop.version>3.2.4</hadoop.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- Scala Library --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!-- Spark Core --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Spark SQL --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Spark Hive Support --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Hadoop Client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies> <build> <plugins> <!-- Scala Compiler Plugin --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.8.1</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> <!-- Maven Compiler Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <!-- Maven Assembly Plugin for creating fat JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 版本不变,给我调整个能用的
最新发布
11-22
你提供的 `pom.xml` 整体结构是正确的,但存在几个 **关键问题** 会导致: - 日志冲突(SLF4J 多绑定) - Hadoop/Spark 兼容性问题 - 打包后运行出错(缺少主类定义、依赖冲突) --- ### ✅ 目标 在 **不改变版本** 的前提下(Scala 2.12.11, Spark 3.1.1, Hadoop 3.2.4),调整 `pom.xml` 使其: 1. 能正常编译 Scala 代码 2. 消除 SLF4J 冲突警告 3. 打包成可执行的 fat jar(包含依赖) 4. 支持本地运行 Spark + Hive 5. 避免常见类加载冲突 --- ### ✅ 修改后的完整 `pom.xml` ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>scala_tutorial</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scala.version>2.12.11</scala.version> <spark.version>3.1.1</spark.version> <hadoop.version>3.2.4</hadoop.version> <main.class>MySQLToHiveIncremental2</main.class> <!-- 主类名 --> </properties> <dependencies> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- Scala Library --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!-- Spark Core --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.12</artifactId> <version>${spark.version}</version> <exclusions> <!-- 排除日志实现冲突 --> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <!-- 避免与 Hadoop 中的 guava 版本冲突 --> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency> <!-- Spark SQL --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.12</artifactId> <version>${spark.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-reload4j</artifactId> </exclusion> </exclusions> </dependency> <!-- Spark Hive Support --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Hadoop Client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <exclusions> <!-- 统一日志框架 --> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-reload4j</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <!-- 使用项目中统一的 Guava --> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency> <!-- 统一使用 log4j-over-slf4j + reload4j 实现 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>ch.qos.reload4j</groupId> <artifactId>reload4j</artifactId> <version>1.2.18.3</version> </dependency> <!-- 将 log4j API 桥接到 slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.30</version> </dependency> <!-- Google Guava(避免多个版本冲突) --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.0-jre</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <!-- Scala Compiler Plugin --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.8.1</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> <!-- Maven Compiler Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <!-- Maven Assembly Plugin: 构建可执行 fat jar --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>${main.class}</mainClass> <!-- 设置主类 --> </manifest> </archive> <finalName>scala_tutorial</finalName> <appendAssemblyId>false</appendAssemblyId> <!-- 不加 -jar-with-dependencies --> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> ``` --- ### ✅ 关键修改点解释 | 修改项 | 说明 | |------|------| | `<packaging>jar</packaging>` | 明确打包类型 | | `<sourceDirectory>` | 告诉 Maven 正确扫描 Scala 源码目录 | | 排除 `slf4j-log4j12`, `slf4j-reload4j`, `log4j` 等冲突依赖 | 防止“Multiple SLF4J bindings”错误 | | 添加 `log4j-over-slf4j` + `reload4j` | 统一日志输出机制 | | 统一引入 `guava` 并排除其他模块中的版本 | 防止 `NoSuchMethodError` | | 在 `maven-assembly-plugin` 中设置 `<mainClass>` | 生成的 JAR 可直接用 `java -jar xxx.jar` 运行 | | `<appendAssemblyId>false</appendAssemblyId>` | 输出文件名为 `scala_tutorial.jar` 而不是 `xxx-jar-with-dependencies.jar` | --- ### ✅ 编译和运行方式 #### 1. 编译打包: ```bash mvn clean package ``` ✅ 输出:`target/scala_tutorial.jar` #### 2. 运行(IDEA 或命令行): ```bash java -cp target/scala_tutorial.jar MySQLToHiveIncremental2 ``` 或直接运行: ```bash spark-submit --class MySQLToHiveIncremental2 target/scala_tutorial.jar ``` --- ### ✅ 补充建议 1. **添加 `log4j.properties` 文件** - 在 `src/main/resources/` 下新建文件:`log4j.properties` - 内容见上一个回答,否则仍有 `No appenders could be found` 警告 2. **确保你的主类中有 `.master("local[*]")`** ```scala val spark = SparkSession.builder() .appName("MySQLToHive") .master("local[*]") // 必须有! .enableHiveSupport() .getOrCreate() ``` --- ### ✅ 成功标志 当你看到以下日志时,表示成功启动: ``` INFO SparkContext: Running Spark version 3.1.1 INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://xxx:4040 ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值