Build Configurations in Swift 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:
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:
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
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:
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. |
<<: Cocos Chengdu Developer Salon unveils the mystery of cocos 3D functions and H5
>>: Testin releases free internal testing tool Pre.im to create a Chinese version of TestFlight
From June 5th to 6th, the 2018 (9th) Global Autom...
The YouTube video platform is not like other soci...
Chengdu Tea Selection Agent Appointment Arrangeme...
Today I will share with you 80 Douyin e-commerce ...
Douyin Kuaishou Ancient Painting Singing Money-Ma...
Apple launched five new MacBook Pro Retina models ...
The US$1 billion "meeting gift" that Xi...
Undoubtedly, the greatest contribution of the bir...
Friends who are engaged in Baidu bidding promotio...
Under the influence of factors such as consumptio...
For consumers, the author of this article propose...
Audit expert: Sun Tao National Psychological Coun...
To judge whether a car is easy to drive, in addit...
Facebook is most afraid of users leaving. It keep...
When we observe the Earth from space, we see a be...