开发者

How to have conditional code depending on active configuration?

开发者 https://www.devze.com 2023-01-15 16:17 出处:网络
I want to have conditional code in my iPhone app depending o开发者_Go百科n configuration (Debug/Release/Distribution). I don’t think Xcode communicates the project configuration somehow to my code, e

I want to have conditional code in my iPhone app depending o开发者_Go百科n configuration (Debug/Release/Distribution). I don’t think Xcode communicates the project configuration somehow to my code, e.g there isn’t a macro or such available, is there?

The best solution I’ve come up with so far: in project settings, for each configuration, define a flag in "Other C flags" like -DDEBUG, -DDISTRIBUTION etc.

Then, in my code, have conditional code with preprocessor macros, like

#ifdef DEBUG
// debug-configuration-specific code here
#endif
#ifdef DISTRIBUTION
// distribution-configuration-specific code here
#endif

Is there a different/better/more elegant way of doing the same?

As to why this is necessary: I am setting up some configuration at runtime depending on configuration. E.g I am working against an HTTP API, and I have a different API endpoint URL for debug and release targets, which I am setting up this way.


Nope ! That's the official recommended way and I don't know any other.


I would add these to User-Defined section of the Build Settings for that target. You can do this by going to Editor -> Add Build Setting -> Add User-Defined Setting while the target is selected.

So basically what you said but in a little different way.

EDIT: Erm I rechecked my project and they are actually set in Apple LLVM 5.0 Preprocessing and not User-Defined. E.g. DEBUG=1

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号