How can I add an #ifdef DEBUG to Xcode?

2020-05-22 02:14发布

问题:

I have some code in a project which should never be used in the release build, but is useful when testing. I'd like to do something like this:

#ifdef DEBUG
    // Run my debugging only code
#endif

Where do I add the DEBUG setting in Xcode 4? I tried putting it in the "Edit Scheme" under Run MyApp->Arguments Passed On Launch, but it didn't work. Alternatively, is there a flag already available for this?

回答1:

In recent Xcode project templates there’s already a DEBUG=1 macro defined for the Debug build configuration (in the Preprocessor Macros section). You can test it using the #if preprocessor directive.



回答2:

I usually add my -DDEBUG=1 to the OTHER_C_FLAGS section in my XCode 4 project's build settings.

And yes, they can even discriminate between Debug / Release / ADHOC / Store builds.