Use build configuration in Swift to support conditional compilation-B

Source: Internet
Author: User

In Objective-c, we often use preprocessing directives to help us execute different code based on different platforms to allow our code to support different platforms, such as:

123456789 #if TARGET_OS_IPHONE    #define MAS_VIEW UIView              #elif TARGET_OS_MAC     #define MAS_VIEW NSView#endif

In Swift, because there is no objective-c for C-language support (temporarily unaware of Swift's 2 to C support), we are not able to use preprocessing directives as comfortably and comfortably as in objective-c.

However, Swift also provides its own way to support conditional compilation, which is to use build configurations (build configuration). Build configurations already contains the literal true and false, as well as two platform test functions OS () and arch ().

where OS () is used to test the system type, the parameters that can be passed in include OSX, IOS, WatchOS, so the above code can be changed in swift:

12345 #if os(iOS)    typealias MAS_VIEW = UIView#elseif os(OSX)    typealias MAS_VIEW = NSView#endif

Note: In the "Sharing code between IOS and OS X" section of WWDC 2014 (session 233), Elizabeth Reid calls this method shimming

Unfortunately, the OS () only detects the system type and cannot detect the version of the system, so the work can only be done at runtime. on how to detect the version of the system, MATTT Thompson boss gave us the answer in its Swift system version checking article.

Let's look at Arch () again. Arch () is used to test the architecture of the CPU, which can pass in values including x86_64, ARM, arm64, i386. It is important to note that arch (arm) does not return true for ARM 64 devices. Arch (i386) returns True when compiled on a 32-bit iOS emulator.

If we want to customize some of the compile configuration options that are used during debugging, you can use the-D flag to tell the compiler what to do in "Build Setting" –> "Swift Compiler-custom Flags" –> "other Swift Flags "–>" Debug to add the required configuration options. If we want to add a common degub option, you can include "-D DEBUG" here. This allows us to perform some of the different operations of debug and release in the code, such as

12345 #if DEBUG    let totalSeconds = totalMinutes#else    let totalSeconds = totalMinutes * 60#endif

A simple conditional compilation declaration is as follows:

12345 #if build configuration    statements#else    statements#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 && | | operator to combine multiple build configuration, and you can use the! operator to reverse the build configuration as follows:

1234567 #if build configuration && !build configuration    statements#elseif build configuration    statements#else    statements#endif

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

Reference

    • Cross-platform Swift

    • Shimming in Swift

    • Swift System Version Checking

    • Interacting with C APIs

Use build configuration in Swift to support conditional compilation-B

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.