Bottom side includes headers in every source file (Objective-C)
Currently I am including my AppDelegate.h
and all my category headers in each of my source files using the prefix header MyApp_Prefix.pch
instead of manually #import
entering them only where they are used. The category methods and a lot of the compiler #define
in my delegate section are used in many places in my code.
Is there any downside to this? Is it just that the compilation will take longer?
a source to share
Source level dependencies. By effectively importing headers into every source file, this means that every source file potentially depends on it, but the dependency is not displayed. Source level dependencies are insidious. They can sneak up when you are not looking, and then it is difficult to untangle them. They can introduce other nasty things, such as file ordering problems.
OTOH for small projects this may not be a big problem many times.
However, for me personally, I'm used to never hooking on another source file unnecessarily - this means that my header files rarely #import other (non-framework) headers. In Objective-C, you can do this more cleanly than, say, C ++, because you always hold Objective-C objects by pointer, so they only need a forward declaration in the header.
a source to share
Is there any downside to this? Is it just that the compilation will take longer?
No, not at all. This means that every time you make changes to any title, the entire project is recompiled. But on the other hand, since it is precompiled, there may be a save in case you need to recompile but not change headers.
The advantage is to stuff all your headers into a pch file because that means you are less likely to skip a header accidentally. Sometimes Objective-C will not compile things correctly if it doesn't have a header file for example. if you have two classes with declared methods:
-(void) doSomethingWith: (float) aNumber;
and
-(void) doSomethingWith: (double) aNumber;
without the right headers, Objective-C can guess the wrong doSomethingWith: and pass a double where a float is expected, or vice versa.
Having said all this, I never really worry about pch. I tend to only import headers in .m files where possible and forward to declare classes in header files.
a source to share