How to use Preprocessor Statement in Swift Project. Skills of high-paying iOS developers.

What is Preprocessor Statement?

#define PI 3.14
#include filename
#if condition

Preprocessor Usecases in Swift

print("print on debug environment only")
#if os(iOS)
print("print on iOS only")
#elseif os(macOS)
print("print on macOS only")
#elseif os(watchOS)
print("print on watchOS only")

Create your own Flag

Go to project build settings and find custom flags
print("Run this line on `DEBUG`")
I’m adding ‘RELEASE` as my custom flag
#if DEBUG 
print("Run this on DEBUG")
#elseif RELEASE
print("Run this on RELEASE")
I’m adding ‘HELLO’ flag just for fun
print("Run this command on HELLO")
print("This means I run on DEBUG too, because I'm on the same envrionment")

What is Other Swift Flags?

Other Swift flags
Adding -DFLAG1, -DFLAG2
#if FLAG1 
#elseif FLAG2




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!