如果#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/