Search code examples
iosxcodeframeworkspch

How to prevent the need for importing a framework in each class?


I have a framework called Parse, which is used in almost each Viewcontroller that I have. How can I prevent the need to put an import statement in each class, like #import Parse/Parse.h ??

I heard you should include it in the PCH file, but somehow that doesn't work. Do I need to set the PCH in 'compile sources', or something like that?


Solution

  • Prefix headers are compiled and stored in a cache, and then automatically included in every file during compilation. This can speed up compilation, and lets you include a file without adding an import statement to every file using it.

    Example:

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

    Add it in your NameOfMyProject-prefix.pch file.