Using Swift compiler custom flags

We can instruct the compiler to include or exclude sections of code according to which build configuration is being used. This is used for a range of purposes, some of the more common being setting differing server addresses for production and test versions of the app and third-party API keys, which can also differ according to build config.

This is important for the solo developer, and absolutely essential for work with enterprise clients. Fortunately, it's very simple to do; there are only two steps involved:

  • Create a Swift compiler custom flag in the build settings
  • Test for this flag in code, and respond appropriately

The compiler will handle the rest.