ios 在 Objective-C 中,在每个类中导入相同的头文件会使编译时间变长吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10910065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:39:11  来源:igfitidea点击:

In Objective-C, importing same headers in every class make compile time longer?

iphoneobjective-ciosheader

提问by js_

I'm a beginner of Objective-C/iOS programing.

我是 Objective-C/iOS 编程的初学者。

I want make a one header file which includes all class headers I use in my project.
And import the header in every class header file.

我想制作一个头文件,其中包含我在项目中使用的所有类头文件。
并在每个类头文件中导入头文件。

Like this question:
Including multiple classes in the same header file

喜欢这个问题:
在同一个头文件中包含多个类

But does this approach increase compile time?
Or are there any other disadvantage?

但是这种方法会增加编译时间吗?
或者还有什么其他的缺点?

Please tell me the good ways to import headers.

请告诉我导入标题的好方法。

回答by Shaggy Frog

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

一般来说,新生成的 iOS 项目都带有这个功能,称为预编译头前缀头,是一个扩展名为.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. .mfiles).

你可以把你想要的所有头文件放在那里,Xcode 会在它构建其他任何东西之前预编译它,并使用它来编译你项目中的其他编译单元(例如.m文件)。

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.

然而,将预编译的头文件视为一个大垃圾场并不一定是好的做法,因为当您可能想要强制执行组件之间的松散耦合时,您的编译单元可能对各种内容形成隐式依赖关系。

回答by user151019

The issue with all the classes in one header is that every time you change a class header then all the files including it even indirectly will need to be recompiled, whilst if you only import the needed class and also use @class when you can then only the files that directly use the class need to be recompiled. Thus in the first case there will be many more compilations than in the latter. This is the way I would recommend to start.

一个头文件中的所有类的问题是,每次更改一个类头文件时,包括它在内的所有文件都需要重新编译,而如果您只导入所需的类并使用@class,那么您只能直接使用类的文件需要重新编译。因此,在第一种情况下,编译数量将比后者多得多。这是我建议开始的方式。

However when your code becomes more stable and classes do NOT change then putting them all in one header can improve the compile time as the precompiled header will contain the same information for each file. What I would do is when the code is not changing so much is put the mature classes into a Framework and the Framework header will include all these classes.

但是,当您的代码变得更加稳定并且类不会更改时,将它们全部放在一个头文件中可以缩短编译时间,因为预编译的头文件将为每个文件包含相同的信息。我会做的是,当代码没有太大变化时,将成熟的类放入框架中,框架头将包含所有这些类。

回答by hacker

You can import that header file in your projects prefix_pch file.then You can use it in Your classes .

您可以在您的项目 prefix_pch 文件中导入该头文件。然后您可以在您的课程中使用它。

回答by skram

If you want headers imported globally you should do so in the YourProject-Prefix.pchfile. It should look something like this..

如果要全局导入标题,则应在YourProject-Prefix.pch文件中执行此操作。它应该看起来像这样..

#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>
   #import "YourGlobalHeader.h"
#endif

Now, All of your classes have YourGlobalHeader.hautomagically imported.

现在,您的所有课程都已YourGlobalHeader.h自动导入。

回答by ThomasW

Putting all the headers in one file may improve build performance in certain cases, but you probably won't notice the difference.

在某些情况下,将所有标头放在一个文件中可能会提高构建性能,但您可能不会注意到其中的区别。

It is better to keep your class headers in different files for purposes of organization. Also, if you are only including the headers that you need in your source files then your build time will be reduced, although again not noticeably if you are using a decent build machine.

出于组织目的,最好将类标题保存在不同的文件中。此外,如果您只在源文件中包含所需的头文件,那么您的构建时间将减少,尽管如果您使用的是体面的构建机器,则同样不会很明显。