public

Swift: Conditional Compilation

Debug spew is a great support mechanism for tracking workflow of your code and to augment debugging. However, leaving that support within your code is not just a poor decor

6 years ago

Latest Post Simply put. by Mario Esposito public

Debug spew is a great support mechanism for tracking workflow of your code and to augment debugging. However, leaving that support within your code is not just a poor decor for code stylists, it can also lead to security issues and occasionally to App Review Rejections.

As of Swift 2.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:

It would be used as the following example:

func obtain() -> AbstractThing {    if _isDebugAssertConfiguration() {        return DecoratedThingWithDebugInformation(Thing())    } else {        return Thing()    }}

Compared with preprocessor macros,

In addition, you can augment your code with markers that make your navigation through those prints and asserts easier, through the usage of MARK, TODO OR FIXME.

i. MARK : //MARK: viewDidLoad

This will create a horizontal line with functions grouped under viewDidLoad

ii. TODO : //TODO: - viewDidLoad (shown below on the jump bar)

This will group function under TODO: - viewDidLoad category

iii. FIXME : //FIXME - viewDidLoad

This will group function under FIXME: - You guessed what it does ;-)

References

Mario Esposito

Published 6 years ago