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?
XCODE 9 AND ABOVE
Yes you can do it.
In Swift you can still use the "#if/#else/#endif" preprocessor macros (although more constrained), as per Apple docs. Here's an example:
Now, you must set the "DEBUG" symbol elsewhere, though. Set it in the "Swift Compiler - Custom Flags" section, "Other Swift Flags" line. You add the DEBUG symbol with the
-D DEBUG
entry.As usual, you can set a different value when in Debug or when in Release.
I tested it in real code and it works; it doesn't seem to be recognized in a playground though.
You can read my original post here.
IMPORTANT NOTE:
-DDEBUG=1
doesn't work. Only-D DEBUG
works. Seems compiler is ignoring a flag with a specific value.In Swift projects created with Xcode Version 9.4.1, Swift 4.1
works by default because in the Preprocessor Macros DEBUG=1 has already been set by Xcode.
So you can use #if DEBUG "out of box".
By the way, how to use the condition compilation blocks in general is written in Apple's book The Swift Programming Language 4.1 (the section Compiler Control Statements) and how to write the compile flags and what is counterpart of the C macros in Swift is written in another Apple's book Using Swift with Cocoa and Objective C (in the section Preprocessor Directives)
Hope in future Apple will write the more detailed contents and the indexes for their books.
Xcode 8 and above
Use Active Compilation Conditions setting in Build settings / Swift compiler - Custom flags.
ALPHA
,BETA
etc.Then check it with compilation conditions like this:
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
)(not available on Swift 3+)_isReleaseAssertConfiguration()
(true when optimization is set to-O
)_isFastAssertConfiguration()
(true when optimization is set to-Ounchecked
)e.g.
Compared with preprocessor macros,
-D DEBUG
flag to use it✗ 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)
@testable
attribute, fate uncertain on future Swift.✗ Using in if/else will always generate a "Will never be executed" warning.
A major change of
ifdef
replacement came up with Xcode 8. i.e use of Active Compilation Conditions.Refer to Building and Linking in Xcode 8 Release note.
New build settings
New setting:
SWIFT_ACTIVE_COMPILATION_CONDITIONS
Previously, we had to declare your conditional compilation flags under OTHER_SWIFT_FLAGS, remembering to prepend “-D” to the setting. For example, to conditionally compile with a MYFLAG value:
The value to add to the setting
-DMYFLAG
Now we only need to pass the value MYFLAG to the new setting. Time to move all those conditional compilation values!
Please refer to below link for more Swift Build Settings feature in Xcode 8: http://www.miqu.me/blog/2016/07/31/xcode-8-new-build-settings-and-analyzer-improvements/