简体   繁体   中英

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

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

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.

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 (eg .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.

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.

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.

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

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

#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.h automagically imported.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM