IOS Swift Conditional Compilation statement

Source: Internet
Author: User

In the C-system language, you can use #if or #ifdef something like a compilation conditional branch to control which code needs to be compiled, and which code does not. There is no concept of macro definition in Swift, so we cannot use #ifdef a method to check whether a symbol is defined by a macro. But in order to control the compilation process and content, Swift also provides us with several simple mechanisms to tailor the content to the requirements.

The first is #if this set of compiler tags or exist, the use of the syntax is not different from the original:

#if <condition>

#elseif <condition>

#else

#endif

But the condition in these expressions are not arbitrary. Swift has built a combination of platforms and architectures to help us compile different code for different platforms, specifically:

Method Optional Parameters
OS () OSX, IOS
Arch () x86_64, ARM, arm64, i386

Note that these methods and parameters are case-sensitive. For example, if we unify our color-related APIs on the IOS platform and MAC platform, one possible way is to match Typealias for conditional compilation:

#if os(OSX)    typealias Color = NSColor#else typealias Color = UIColor#endif

In addition arch() to the parameters need to explain is arm and arm64 two respectively corresponding to 32-bit CPU and 64-bit CPU of the real machine situation, and for the simulator, the corresponding 32-bit device simulator and 64-bit device simulator corresponding to the difference is i386 and x86_64 , They also need to be treated separately.

Another way is to conditionally compile the custom symbols, for example, we need to use the same target to complete the same app in charge version and free version of two versions, and want to click on a button to pay the version of the function, and the free version pop-up prompts, you can use a similar method:

@IBAction func someButtonPressed(sender: AnyObject!) { #if FREE_VERSION // 弹出购买提示,导航至商店等 #else // 实际功能 #endif}

Here we use FREE_VERSION this compilation symbol to represent the free version. To make it work, we need to set in the project's compilation options, find the Swift Compiler-custom flags in the project build Settings, and in which the other swift flags are added -D FREE_VERSION .

IOS Swift Conditional Compilation statement

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.