Why Isn't Projectname-Prefix.Pch Created Automatically in Xcode 6

Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?

I suspect because of modules, which remove the need for the #import <Cocoa/Cocoa.h>.

As to where to put code that you would put in a prefix header, there is no code you should put in a prefix header. Put your imports into the files that need them. Put your definitions into their own files. Put your macros...nowhere. Stop writing macros unless there is no other way (such as when you need __FILE__). If you do need macros, put them in a header and include it.

The prefix header was necessary for things that are huge and used by nearly everything in the whole system (like Foundation.h). If you have something that huge and ubiquitous, you should rethink your architecture. Prefix headers make code reuse hard, and introduce subtle build problems if any of the files listed can change. Avoid them until you have a serious build time problem that you can demonstrate is dramatically improved with a prefix header.

In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.

EDIT: To your specific question about a HUD you use in all your view controllers, yes, you should absolutely import it into every view controller that actually uses it. This makes the dependencies clear. When you reuse your view controller in a new project (which is common if you build your controllers well), you will immediately know what it requires. This is especially important for categories, which can make code very hard to reuse if they're implicit.

The PCH file isn't there to get rid of listing dependencies. You should still import UIKit.h or Foundation.h as needed, as the Xcode templates do. The reason for the PCH is to improve build times when dealing with really massive headers (like in UIKit).

PCH File in Xcode 6

Add the pch file which is under Others in File-New...and don't forget to add it to your LLVM6.0 - Language section of Build Settings as Project/whateveryounamedyourpchfile.pch

.pch File in Xcode 6.1

  1. You'll need to create a PCH file named ProjectName-Prefix.pch. In the new file dialog you'll find PCH under Other.
  2. Go to Project > Build Settings and search "Prefix Header". Under "Apple LLVM 6.0 - Language" add your PCH file: ProjectName/ProjectName-Prefix.pch note: make sure you select 'All' not 'Basic'
  3. Now add any headers you want globally included in your PCH file
  4. Clean and rebuild your project.

Why .pch file not available in swift?

Even in Obj-C, using Macros for constants and expressions is something you shouldn't do. Taking examples from your comments:


It would be better as a category method on UIColor, for example

+ (UIColor *) navigationBarColor {
return [UIColor colorWith...];

The isPad macro should be either a plain function

BOOL isIPad() {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

or again a category method, e.g. on UIDevice

[UIDevice isIPad]

defined as

+ (BOOL)isIPad {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

The precompiled headers were never meant to share macros to all of your code. They were made to include framework headers there to speed up the compilation process. With the introduction of modules last year and now with the possibility to create custom modules, you don't need precompiled headers any more.

In Swift the situation is the same - there are no headers and no macros so there is also no precompiled header. Use extensions, global constants or singletons instead.

Related Topics

Leave a reply