1

This question is related to this: Constants in Objective-C

I would like to add pre compiled headers to my project to store app constants. I want to this as an alternative to having a constants.h file and importing it wherever it's needed. Is my thinking correct here?

On the above thread a guy mentioned modifying "YourAppNameHere-Prefix.pch".. I'm not sure this is a good approach.

How exactly can I create my own *.pch file and add it to my Xcode project so I can store application level constants?

I have tried googling/searching here for this but I'm just getting Objective-C++ and Clang stuff.. I'm not sure this is relevant.

Cheers, Conor

Community
  • 1
  • 1
jim
  • 8,670
  • 15
  • 78
  • 149
  • Why `YourAppNameHere-Prefix.pch` is bad approach? This file is already contains standard headers and you also can put your header. – beryllium Oct 07 '11 at 10:07
  • I just thought because the file was auto generated by Xcode. I thought that a safe/better practice approach would be to generate another .PCH file for user added constants. I could be wrong on this though. Whats the best approach for app level constants? I already have a GlobalVars singleton class. Should I just stick them in there? – jim Oct 07 '11 at 10:18

1 Answers1

2

My answer probably not just in time, and especially for Xcode 6 but i hope i'll help other, who already want's to create it's own constant.h file and set it on all of project objects.

So

  1. Create constant.h file by NSObject. Put all of constants you need in .h file before @interface:

    #import <...>
    
    #define kSomeFirstConstant 1 //where 1 is an integer value
    #define kSomeSecondConstant 2 //where 2 is an integer value
    
    @interface Constant: NSObject
    
  2. Create precompiled header file .pch (eg: precompiledFile.pch)

  3. Put #import "constant.h" in .pch file right between #define and #endif:

    #define ..._pch
    
    #import "constant.h"
    
    #endif
    
  4. Configure prefix name and some other options in Project navigator, Building Settings of your project target:

Apple LLVM 6.0 Language

Increase sharing of Precompiled headers - NO

Precompile prefix header - YES

Prefix Header - TargetName/precompiledFile.pch (eg: MyTarget/precompiledFile.pch)

After that, run building process and all of your constant in constant.h file will be accessible in all of your project objects. Also read this: http://qualitycoding.org/precompiled-headers/

Andrew
  • 671
  • 2
  • 8
  • 21