Swift Tip: Use build configurations to support conditional compilation

Swift Tip: Use build configurations to support conditional compilation

In Objective-C, we often use preprocessor directives to help us execute different codes according to different platforms, so that our code supports different platforms, such as:

  1. # if TARGET_OS_IPHONE
  2.  
  3. #define MAS_VIEW UIView
  4.  
  5. #elif TARGET_OS_MAC
  6.  
  7. #define MAS_VIEW NSView
  8.  
  9. #endif

In Swift, since the support for C language is not as friendly as Objective-C (I don’t know how Swift 2 supports C yet), we cannot use preprocessing instructions as freely and comfortably as in Objective-C.

However, Swift also provides its own way to support conditional compilation, using build configurations. Build configurations already include literals true and false, as well as two platform test functions os() and arch().

os() is used to test the system type. The parameters that can be passed in include OSX, iOS, and watchOS. So the above code can be changed to:

  1. # if os(iOS)
  2. typealias MAS_VIEW = UIView
  3. #elseif os(OSX)
  4. typealias MAS_VIEW = NSView
  5. #endif

Note: In the WWDC 2014 session “Sharing code between iOS and OS X” (session 233), Elizabeth Reid refers to this approach as shimming.

Unfortunately, os() can only detect the system type, but not the system version, so these tasks can only be handled at runtime. As for how to detect the system version, Mattt Thompson gave us the answer in his article Swift System Version Checking.

Let's take a look at arch(). arch() is used to test the CPU architecture. The values ​​that can be passed in include x86_64, arm, arm64, and i386. It should be noted that arch(arm) will not return true for ARM 64 devices. However, arch(i386) will return true when compiled on a 32-bit iOS simulator.

If we want to customize some compilation configuration options used during debugging, we can use the -D flag to tell the compiler. The specific operation is to add the required configuration options in "Build Setting" -> "Swift Compiler-Custom Flags" -> "Other Swift Flags" -> "Debug". If we want to add commonly used DEGUB options, we can add "-D DEBUG" here. In this way, we can perform some different operations in the code for debug and release, such as

  1. # if DEBUG
  2. let totalSeconds = totalMinutes
  3. # else  
  4. let totalSeconds = totalMinutes * 60  
  5. #endif

A simple conditional compilation statement looks like this:

  1. # if build configuration
  2. statements
  3. # else  
  4. statements
  5. #endif

Of course, statements can contain 0 or more valid Swift statements, which can include expressions, statements, and control flow statements. In addition, we can also use the && and || operators to combine multiple build configurations, and we can use the ! operator to negate the build configuration, as shown below:

  1. # if build configuration && !build configuration
  2. statements
  3. #elseif build configuration
  4. statements
  5. # else  
  6. statements
  7. #endif

It is important to note that in Swift, conditional compilation statements must be syntactically valid, because Swift will syntax check them even if the code will not be compiled.

refer to

Cross-platform Swift

Shimming in Swift

Swift System Version Checking

Interacting with C APIs

<<:  New mobile page inspection tool: Mobile Checker

>>:  Want to be an independent freelance developer? It’s always hard to get started

Recommend

IEA: India's natural gas market outlook report to 2030

India’s gas demand is expected to grow by nearly ...

Community operation: operation skills of community content!

Content is the prerequisite for the development o...

How to achieve user growth from the perspective of user retention goals?

Retention is the goal of many product operations ...

Revealing the alternative method of using Apple loopholes for promotion

Have you ever been annoyed by those annoying bann...

Sorry, the other party withdrew an eagle...

Crested Eagle The Crested Eagle is a national sec...

User operation: AARRR user model and traffic pool model explained in detail!

1. What is the AARRR user analysis model? AARRR i...

The paints are not only colorful, but some are also very "heavy"

Did you know that some paints are not as glamorou...

4 steps to make users addicted to your product instantly!

How to make users addicted to you? Why do users k...

Brand promotion: How to make data brands generate value!

Data is an asset. For online brand promotion , th...