Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 921
js_ Avatar asked Jun 06 '12 07:06

js_


3 Answers

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.

like image 126
Shaggy Frog Avatar answered Sep 18 '22 05:09

Shaggy Frog


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.

like image 38
mmmmmm Avatar answered Sep 19 '22 05:09

mmmmmm


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

like image 43
hacker Avatar answered Sep 18 '22 05:09

hacker