Xcode preprocessing with Custom flags in Swift

Tsungyu Yu
2 min readJun 8, 2019

--

Photo by Hamish Weir on Unsplash

It has been a long time since I became a developer, really enjoy seeking problems and finding a good workflow.

So this post is about the #if , the preprocessing of the swift.

The famous key to using #if is the DEBUG, but what if we just want to test on some case like #if MOCKDATA, Here are only 4 steps to using that:

  1. Go to Project page.
  2. Choose the target to change. In this case, we are in the command line tool project, and we only a target, which is preprocessingDEMO.
  3. Open the Build Settings, right at the top of the project page. Make sure you are doing step 2 the right way.
  4. Find the Column of Swift Compiler — Custom Flags, and add the phase and the key you what to set. In this case, we are adding MOCKDATA in the Debug condition.
Quick note for setting up custom flags.

There you go! Now you can use #if MOCKDATA to test whatever you what.

--

--