This gist provides a wrapper function for Swift conditional compilation statements. If you ever want some code to run in production builds only, this gist might be for you.
For no reason. This is just useless! :P
Jk, not really, and here is why:
- I hate macro. And the like. I don't want to see them in my code. Or at least see them too much. Chances are, you hate them too. So better put them all in one (or two) place.
- It helps early detect compilation errors!!! Seriously,
#if !DEBUG
// some code should only run in production
utrhoerheugmnaknjiftsyhmdugsottdhieeo // some code your cat wrote and committed while you're sleeping (or drunk)
#endif
The code piece above will compile. It DOES!!! Until you are about to ship your app. So by putting the conditional compilation code outside of the conditional compilation statements, you always know if your cat has secretly modified your code.
To use the function, you have first to define a custom Swift compiler flag in your Xcode project:
- Go to the
Build Settings
tab of your target or project. - Look for the
Other Swift Flags
setting inSwift Compiler - Custom Flags
section. - Expand the setting, you should now see
Debug
andRelease
. - Add
-DDEBUG
toDebug
.
In case you don't use Xcode project, pass -D DEBUG
to the command line build statement.
ifNotDebugDo {
print("NOT debug")
}