0

I heard that the .pch is good for putting macros and constants definitions, so for its default content:

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif

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

where should the definitions be put in? It is strange that it seems to be importing UIKit.h, but ViewController.h also import that same file (I thought the .pch is imported by all files by default? So ViewController.hshouldn't need to import UIKit.h again)

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    Your way is correct, take a look at this question http://stackoverflow.com/questions/2845211/iphone-prefix-pch-best-practices – self Jun 02 '12 at 08:35

2 Answers2

2

Precompiled headers are meant to improve overall compile times for projects, so the design of precompiled headers is entirely driven by performance concerns. The use case for precompiled headers is relatively simple: when there is a common set of headers that is included in nearly every source file in the project, we precompile that bundle of headers into a single precompiled header (PCH file).

The .pch file allows you to import files like UIKit.h and Foundation.h(rather than importing them in every .h of your project) If you have those files imported in the .pch, in your own classes don't need to import them.

The significance of #ifdef OBJC is so that you don't import headers containing objective-c code if you don't have the compiler set to build objective c code (hence avoiding lots of compiler errors).

self
  • 1,205
  • 1
  • 14
  • 17
1

Any extra imports I usually put in with:

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

You are correct, there is no need to import one of those again, but there is no harm in doing so.

James Webster
  • 31,873
  • 11
  • 70
  • 114