4

In reference to this related question on stackoverflow:

If you create a constants file, how do you "link" to it in your target, so you don't have to

#import "Constants.h"

in every file you use constants?

Community
  • 1
  • 1
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154

3 Answers3

7

You really should be using #import "Constants.h" every place you want to use the constants within it; Objective-C is a C-based language.

Furthermore, you aren't "linking" to it either when you put an #import directive in your code or if you put one in your prefix file. In both cases, the contents of the file are included in the text stream fed to the compiler by the preprocessor.

Finally, you shouldn't generally add random things to your prefix file. (Panagiotis Korros referred to this as "your pre-compiled header file," but that's slightly incorrect; your prefix file is used to generate the pre-compiled header file.) If you keep your build settings consistent across projects, and use the same name for your prefix files across projects, Xcode will actually cache and re-use the precompiled versions for you very aggressively. This is defeated by putting project-specific contents in them.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
  • Thanks, Chris It's probably bad style to have global constants and I should just be importing when needed. I was just being lazy and figured I shouldn't have to include the constants file when I need it. – Corey Floyd May 13 '09 at 17:13
2

You can put the import line in your pre-compiled header file. That is the .pch file named after you application name.

Panagiotis Korros
  • 10,840
  • 12
  • 41
  • 43
0

When I use the constant in more file inside my application, normally I use the .pch file (find it under "Supporting Files" folder).

Into my .pch file I insert the constant, for example:

static const int NAME_CONSTANT = 200;

and use NAME_CONSTANT into all file inside my project without import never file because the .pch is pre-compiled header file.

Alessandro Pirovano
  • 2,509
  • 28
  • 21