In C/C++/Objective-C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors.
#ifdef DEBUG
// Debug-only code
#endif
Is there a similar solution in Swift?
In C/C++/Objective-C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors.
#ifdef DEBUG
// Debug-only code
#endif
Is there a similar solution in Swift?
isDebug Constant Based on Active Compilation Conditions
Another, perhaps simpler, solution that still results in a boolean that you can pass into functions without peppering
#if
conditionals throughout your codebase is to defineDEBUG
as one of your project build target'sActive Compilation Conditions
and include the following (I define it as a global constant):isDebug Constant Based on Compiler Optimization Settings
This concept builds on kennytm's answer
The main advantage when comparing against kennytm's, is that this does not rely on private or undocumented methods.
In Swift 4:
Compared with preprocessor macros and kennytm's answer,
-D DEBUG
flag to use it✓ Documented, which means the function will follow normal API release/deprecation patterns.
✓ Using in if/else will not generate a "Will never be executed" warning.
After setting
DEBUG=1
in yourGCC_PREPROCESSOR_DEFINITIONS
Build Settings I prefer using a function to make this calls:And then just enclose in this function any block that I want omitted in Debug builds:
The advantage when compared to:
Is that the compiler checks the syntax of my code, so I am sure that its syntax is correct and builds.
![In Xcode 8 & above go to build setting -> search for custom flags ]1
In code