objective-c - 如果在PCH文件中导入UIKit,那么为什么在每个文件中又导入一次呢?

标签 objective-c xcode precompiled-headers

如果#import <UIKit/UIKit.h>#import <Foundation/Foundation.h>都在我们的“ProjectName.pch”文件中找到,使它们全局导入,为什么当Xcode创建新类时它们会自动添加到头文件中?

最佳答案

.pch 是作为编译时优化而存在的,我个人建议确保类仍然可以在没有 pch 的情况下构建(因此仍然手动导入到 h/m 中),这样它就可以 A) 没有 pch 构建,B) 所以如果您重复使用代码,您可以轻松查看其依赖项。

In general, newly-generated iOS projects come with this functionality, which is called a precompiled header or prefix header, and is a file that has the extension .pch.

You can throw all the headers you want in there and Xcode will pre-compile it before it builds anything else, and use it to compile the other compilation units in your project (e.g. .m files).

Using a precompiled header may or may not increase compile time; in general, it reduces compile time, as long as you have a lot of common headers and/or a lot of source files.

However, it's not necessarily good practice to treat the pre-compiled header like a big dumping ground, as your compilation units can form implicit dependencies on all sorts of stuff when you may want to enforce loose coupling between components.

关于objective-c - 如果在PCH文件中导入UIKit,那么为什么在每个文件中又导入一次呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359243/

相关文章:

iphone - UICollectionView 节中的单元格数

objective-c - 如何在 Xcode 中保护我的框架?

objective-c - 何时使用 'self' 访问属性

ios - 在Xcode中导入SQLite时没有找到这样的模块

c - 存档的应用程序无法打开文本文件,但从 Xcode 成功打开

objective-c - 使用 Swift 的 Epson 打印机找不到 arm64 的符号

ios - 警告 : Attempt to present <UIViewController: 0x74acff0> on <ViewController: 0x82b25a0> whose view is not in the window hierarchy

ios - 尝试上传到 Apple App Store 时出现 -23690 代码?

c++ - 在静态库项目中使用预编译头

c++ - Qt项目中如何使用预编译头文件