Ernest
Ernest

Reputation: 824

xcode create constant/variable as a build setting?

I have a URL used multiple times in my code and would like to centralize it into something like a build setting constant/variable. How would I go about accessing a build setting from my code? And is this the right thing to do?

Thank you.

Upvotes: 1

Views: 882

Answers (2)

Metabble
Metabble

Reputation: 11841

What you want to do, essentially, is import a header that defines a constant into every one of your other files. The easiest way to do this is to stick it in (application name)-Prefix.pch in the Supporting Files group in the project navigator. Anything defined in this precompiled header can be used by any other file. From Programming iOS 5 by Matt Neuburg:

The precompiled header is a device for making compilation go faster. It’s a header file; it is compiled once (or at least, very infrequently) and the results are cached (off in /var/folders/) and are implicitly imported by all your code files. So the precompiled header should consist primarily of #import directives for headers that never change (such as the built-in Cocoa headers); it is also a reasonable place to put #defines that will never change and that are to be shared by all your code.

Upvotes: 1

prashant
prashant

Reputation: 1920

Constants.h

static NSString * const myStackURL = @"http://stackoverflow.com/users";

or

#define myStackURL @"http://stackoverflow.com/users"

Upvotes: 1

Related Questions