Fork me on GitHub
Michael Schubert home

Injecting compile time constants into an iPhone project

29 Apr 2009 – New York

So you have your iPhone project and you need to dynamically set constant string referenced in your code from an environment variable (think: for localization and with stringWithFormat substitution). What to do?

Suppose you had an environment variable FOO_BAR with a value of “Hello”

Give it a value of:


-DFOO_BAR=@\"$(FOO_BAR)\ World\"

You can now do:


NSString *titleText = NSLocalizedString(@"Title", nil);
titleText = [NSString stringWithFormat: titleText, FOO_BAR];
[title setText:titleText];

And now title (in my case it is a UILabel) will have text of “Hello World”

Why the xcode project does not define an empty OTHER_CFLAGS build setting by default is beyond me but it is not cool.