Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top