How to use Preprocessor Statement in Swift Project. Skills of high-paying iOS developers.
It is essential skill to make the logic of programming run in different environment. For example, a test environment or a production environment must have different settings. One way is to use the preprocessor statement.
I have studied preprocessor statement in the past. But, although it is an easy concept, it was difficult to find an easy-to-follow article, so I write it myself.
If you are a junior developer, This article can advance your 3 years of career.
What is Preprocessor Statement?
PREPROCESSOR STATEMENT literally means a statement that is processed in advance before compilation.
Preprocessing statements start with #
.
There are many different types,
1) Preprocessor that defines a Constant
#define PI 3.14
2) Preprocessor to include files
#include filename
3) Preprocessor that determines which code to compile
#if condition
source...
#endif
#Warning
Note that #define
and #include
cannot be used in Swift. The above example is mainly used in C language.
Preprocessor Usecases in Swift
1) Preprocessor is used to distinguish between release and debug.
#if DEBUG
print("print on debug environment only")
#endif
2) Preprocessor is used to distinguish OS.
#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
This is where the subject becomes interesting. I usually use preprocessor to distinguish my application’s running environment. Let’s do it together.
1) Go to Build Settings
and search for Custom Flags
Now you can see only DEBUG
flag is defined. It means you can use this flag like this.
#if DEBUG
print("Run this line on `DEBUG`")
#endif
2) Add your own FLAG
If you do that, now you can use #if RELEASE
statement.
#if DEBUG
print("Run this on DEBUG")
#elseif RELEASE
print("Run this on RELEASE")
#endif
3) Adding Additional Flags
After you’ve added your own flag
, you can use it on project.
#if HELLO
print("Run this command on HELLO")
print("This means I run on DEBUG too, because I'm on the same envrionment")
#endif
You’re smart enough to understand flag HELLO
will be executed upon DEBUG
environment.
What is Other Swift Flags?
As you work on big project with big companies, I’m pretty sure you are supposed to work with different many environments. Environment for QA, TEST, DEBUG, DEV and many more…
Using Other Swift Flags
is not necessary. But it helps you organize your custom flags.
The main difference between Active Compilation Conditions
and Other Swift Flags
is that you just need to add -D
when using Swift Flags
.
Now you can use it like this.
#if FLAG1
print("FLAG1 ENVIRONMENT")
#elseif FLAG2
print("FLAG2 ENVIRONMENT")
#endif
Note that you are not forced to use Other Swift Flags
. I only use Other Swift Flags
when I have so many flags that I need to organize them.
Conclusion
When I was just beginning my career as an iOS Developer, I used to comment and uncomment some code before release. If you do this manual job, you are prone to make a mistake. Now, I’m proactively using Pre-processor Statement without worrying about releasing my app with Test Environment.
Next time, I will show you more practical examples.
Please, leave a comment if you have any question. I’m willing to help you.