How to write code for the only the DEBUG build in Swift

February 09, 2019

One of the common things that you may want to do is run code that is only available in the development configuration. For example; a environment selector to switch between dev and prod environment. This can be done by using Macros. Let's see how it works.

// Debug Build Only
let showEnvironmentSelector = true
// Release Build only
let showEnvironmentSelector = false

if showEnvironmentSelector {
   // Update UI

Now go to Edit Schemes -> Switch between Debug and Release, and watch what happens.