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
source...
#endif

Preprocessor Usecases in Swift

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

Create your own Flag

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

What is Other Swift Flags?

Other Swift flags
Adding -DFLAG1, -DFLAG2
#if FLAG1 
print("FLAG1 ENVIRONMENT")
#elseif FLAG2
print("FLAG2 ENVIRONMENT")
#endif

Conclusion

--

--

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!