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

吃可爱长大的小学妹 提交于 2019-12-18 06:24:07

问题


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?


回答1:


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



回答2:


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]];



回答3:


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


来源:https://stackoverflow.com/questions/1406175/different-code-config-in-release-debug-build-obj-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!