-->

How to have conditional code depending on active c

2020-07-17 15:45发布

问题:

I want to have conditional code in my iPhone app depending on 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.

回答1:

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



回答2:

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



标签: iphone xcode