Swapnil
Swapnil

Reputation: 1878

including common header files in .pch file

I am just wondering, is it good practice to include commonly/mostly used header files in .pch file?

e.g Most of files required below headers

    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>

So instead of each file should have, is it good practice to add those in .pch file?

.pch file

     #ifdef __OBJC__
     #import <UIKit/UIKit.h>
     #import <Foundation/Foundation.h>
     #import <MostlyUsed.h>
     #endif

I found some disadvantages of this, 1) Dependancies get hidden. 2) Source files can not be copied directly.

It will be great if you guys provide some inputs on this.

Thanks

Upvotes: 3

Views: 1739

Answers (2)

Sanober Malik
Sanober Malik

Reputation: 2805

It is certainly a good practice as it reduces compile time and code is lot more manageable by doing so.

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Yes you can do it.

Even it reduces compile time.

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

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.

Upvotes: 7

Related Questions