Recently I have interest in C/C++ #ifdef macro features in Swift. The main reason is that I have some debug codes through out my project for test and debug purpose. For example, I like to add deinit
destruction with print()
codes in classes to test memory leaks. Since those destructors have no purpose to clean up resources, I have to comment out them in production compilation.
In Swift, there are some conditional compilation features to including or excluding block of codes. For example, the following codes using #available to test if the current iOS matches required version.
if #available(iOS 11.0, *)
Further investigation, I find out that Swift does support
#if
statements to provide conditional compilation. The following are steps to implement the feature.Define Custom Compilation Flags
The first step is to define customized complication flags in Xcode project. In the section of Build Settings, find Swift Compiler - Custom Flags (make sure All and Combined are enabled on top tab bar).
Then, you can add custom flag variables. Here are two examples of DEBUG_PRINT and DEBUG_DEINIT. For each flag, add -D before it, like "-D DEBUG_DEINIT -D DEBUG_PRINT"
Note: in the picture, I defined those two with _X as suffix to disable them. If I want to enable them in compilation, I remove _X.
Use Custom Compilation Flags in Swift
To use #if feature, it is quite straightforward. For example, there is the conditional compilation block for destructor:
- // MARK: - CTOR
- #if DEBUG_DEINIT
- deinit {
- Logger.debug() {
- String("\(CopyMoveViewController.className) deinit")
- }
- }
- #endif
- class Logger {
- ...
- static func debug(_ message: () -> String) {
- #if DEBUG_PRINT
- print(message())
- #elseif DEBUG_DEINIT
- print(message())
- #endif
- }
- }
Now I don't need to comment out some debug codes when I need to compile my project to production release target. The only thing I need to do is to disable those flags in my Xcode project file.
References
- Swift Documention on Compiler Control Statements
- SO's QA: In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project
0 comments:
Post a Comment