JoJo JoJo - 1 year ago 66
iOS Question

Where to store global constants in an iOS application?

Most of the models in my iOS app query a web server. I would like to have a configuration file storing the base URL of the server. It will look something like this:

// production
// static NSString* const baseUrl = ""

// testing
static NSString* const baseUrl = ""

By commenting out one line or the other, I can instantly change which server my models point to. My question is, what's the best practice for storing global constants in iOS? In Android programming, we have this built-in strings resource file. In any Activity (the equivalent of a UIViewController), we can retrieve those string constants with:

String string = this.getString(R.string.someConstant);

I was wondering if the iOS SDK has an analogous place to store constants. If not, what is the best practice in Objective-C to do so?

Answer Source

You could also do a

#define kBaseURL @""

in a "constants" header file, say constants.h. Then do

#include "constants.h"

at the top of every file where you need this constant.

This way, you can switch between servers depending on compiler flags, as in:

#ifdef DEBUG
    #define kBaseURL @""
    #define kBaseURL @""
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download