As of Swift 4.1, if all you need is just check whether the code is built with debug or release configuration, you may use the built-in functions:
* `_isDebugAssertConfiguration()` (true when optimization is set to `-Onone`)
* <del>`_isReleaseAssertConfiguration()` (true when optimization is set to `-O`)</del> (not available on Swift 3+)
* `_isFastAssertConfiguration()` (true when optimization is set to `-Ounchecked`)
e.g.
func obtain() -> AbstractThing {
if _isDebugAssertConfiguration() {
return DecoratedThingWithDebugInformation(Thing())
} else {
return Thing()
}
}
Compared with preprocessor macros,
* ✓ You don't need to define a custom `-D DEBUG` flag to use it
* ~ It is actually defined in terms of optimization settings, not Xcode build configuration
* ✗ Undocumented, which means the function can be removed in any update (but it should be AppStore-safe since the optimizer will turn these into constants)
* these once [removed](
[To see links please register here]
), but [brought back to public to lack of `@testable` attribute](
[To see links please register here]
), fate uncertain on future Swift.
* ✗ Using in if/else will always generate a "Will never be executed" warning.