Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different code / config in Release & Debug build (Obj-C)

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?

like image 834
Christian Avatar asked Sep 10 '09 16:09

Christian


2 Answers

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]];
like image 115
pix0r Avatar answered Oct 06 '22 01:10

pix0r


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
like image 29
bbum Avatar answered Oct 06 '22 00:10

bbum