It is unclear to me when using compiler directives which of the below two code snippets is correct/preferred and why. It seems that most developers and Open Source projects I\'v
You are correct. #if DEBUG
will not evaluate if DEBUG
is defined as 0
.
As for when to use each, you can stick to using #ifdef
for anything where you only need to add code if the preprocessor definition is present, such as adding debug logging. If you need to inspect the value and go down different compilation paths, then I would use a 0
or 1
. A good example of that is TARGET_IPHONE_SIMULATOR
, which is always defined for an iOS project, but only 1
if you’re compiling for the simulator.