o9s
is a microblog
home archives search feed blogroll


Prefix File

You can use a prefix file to import classes in every classes. For example, if you have -

#import <UIKit/UIKit.h>

- somewhere in your prefix file, you don’t have to import UIKit manually in every file you need it. This is very useful for iOS apps since you’re going to want to have UIKit available in most of your classes. The same goes for Foundation.

It’s fast too.

Prefix.pch is a precompiled header. Precompiled headers were invented to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time.”

- RaffAl on Stack Overflow

To add one to your project, follow these instructions.

  1. Right click on your project name in Xcode.
  2. Choose New file.
  3. Scroll down to the Other section.
  4. Select PCH File and click Next.
  5. Name your file. The convention is YourProjectName-Prefix.pch.
  6. Click on your project name.
  7. Go to your build settings.
  8. Go to the section named Apple LLVM 9.0 - Language”.
  9. Click on Prefix Header
  10. Enter the value $(SRCROOT)/YourProject/YourProjectName-Prefix.pch.

Don’t put macros in your PCH file, it should only contain #imports and #includes. If you need macros, you should define a Common.h file and then import this file from your prefix file. Here’s an example of a Common.h file.

My prefix file looks like this in most of my projects.

#import <Availability.h>

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Common.h"
#endif
Posted on 2018-03-27   #ios  






← Next post    ·    Previous post →