Link to a constant file in Cocoa / Xcode

Regarding this related question in stackoverflow:

If you create a constant file, how do you “link” to it for your purpose, so you don’t need to

#import "Constants.h" 

are constants used in every file?

+4
source share
3 answers

You can put the import string in your precompiled header file. This is the .pch file named after your application.

+2
source

You really need to use #import "Constants.h" every place you want to use constants inside it; Objective-C is a C-based language.

In addition, you do not become attached to it either when installing the #import directive in your code, or if you put it in your prefix file. In both cases, the contents of the file are included in the text stream transmitted by the compiler to the preprocessor.

Finally, you usually do not add random things to your prefix file. (Panagiotis Korros called it “your precompiled header file,” but it's a little incorrect, your prefix file is used to create the precompiled header file.) If you keep your build settings consistent between projects and use the same name for your project prefix files, Xcode will actually cache and reuse precompiled versions for you very aggressively. This wins by adding project-specific content to them.

+7
source

When I use a constant in more files inside my application, I usually use a .pch file (find it in the Support Files folder).

I insert a constant into my .pch file, for example:

 static const int NAME_CONSTANT = 200; 

and using NAME_CONSTANT in the entire file inside my project without importing the file is never a file because .pch is a precompiled header file.

0
source

All Articles