I'm writing a Cocoa app in Objective C that's communicating with a webservice and I want it to connect to a sandbox in debug mode and to the real webservice in release mode. All I need is to change on line of code where an object that holds the configuration gets instantiated (with a different init-message and different parameters).
So how would I swap a line of code for Release or Debug mode?
You could check for #ifdef DEBUG
, but I would recommend you don't do that.
There are a lots of differences between Debug and Release builds. Different compiler optimizations, different sets of symbols, etc...
Invariably, you are going to find yourself in a situation where you want to run the Release build against your sandbox for debugging purposes.... and your debug build against the production webservice because some customer has a problem that only reproduces in Release mode.
So, for that, I'd suggest a user default. See NSUserDefaults
.
Note that simple user defaults can be set from the command line.
Thus, you could do something like:
/path/to/Myapp.app/Contents/Macos/Myapp -ServerMode Debug
You can use config-specific defines to change the code that's executed. Read about how to define a preprocessor symbol in Xcode first. Then, in your code, do something like this:
#if DEBUG_MODE
#define BACKEND_URL @"http://testing.myserver.com"
#else
#define BACKEND_URL @"http://live.myserver.com"
#end
NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:BACKEND_URL]];
First, define a preprocessor symbol that is only set in your Debug build configuration, as per the question 367368 - call it, say, DEBUG. Then you can do
#ifdef DEBUG
// Code that only compiles in debug configuration
#else
// Code that compiles in other configurations (i.e. release)
#endif