Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link to a constants file in Cocoa / Xcode

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?

like image 362
Corey Floyd Avatar asked Jan 23 '23 16:01

Corey Floyd


1 Answers

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.

like image 53
Chris Hanson Avatar answered Jan 26 '23 05:01

Chris Hanson