Prefix Headers Don’t Mean You Can Stop Using #import

Precompiled (or, prefix) headers (.pch files) are a great way to speed up compilation, and also appear to let you save some lines of code; instead of writing #import <Cocoa/Cocoa.h> for every class header that derives from NSObject, you can just import that in the prefix header and be done with it.

This works, but unfortunately it conflates their purpose slightly. This becomes apparent if you build a framework that uses a prefix header and excludes the #import directives from its headers; clients of that framework will miss out on the headers imported in the prefix header, and may not be able to compile until they figure that out!

Another place this can crop up is in your unit test bundles; if your application uses a prefix header to import files not imported in the individual headers, your test suites may fail to compile until you import those files manually.

The good news is, being clever, this probably already occurred to you; if not, you’re still clever, just busy. ☻