Skip to main content

Compile-time Swift version checking

About 2 minSwiftArticle(s)bloghackingwithswift.comswiftswift-2.2

Compile-time Swift version checking 관련

HACKING WITH SWIFT

What's new in Swift?

Compile-time Swift version checking | Changes in Swift 2.2

Compile-time Swift version checking

Available from Swift 2.2

Swift 2.2 adds a new build configuration option that makes it easy to combine code code written in versions of Swift into a single file. This might seem unnecessary, but spare a thought to people who write libraries in Swift: do they target Swift 2.2 and hope everyone is using it, or target Swift 2.0 and hope users can upgrade using Xcode?

Using the new build option lets you write two different flavours of Swift, and the correct one will be compiled depending on the version of the Swift compiler.

For example:

#if swift(>=2.2)
print("Running Swift 2.2 or later")
#else
print("Running Swift 2.1 or earlier")
#endif

Just like the existing #if os() build option, this adjusts what code is produced by the compiler: if you're using a Swift 2.2 compiler, the second print() line won't even be seen. This means you can use utter gibberish if you want:

#if swift(>=2.2)
print("Running Swift 2.2 or later")
#else
THIS WILL COMPILE JUST FINE IF YOU'RE
USING A SWIFT 2.2 COMPILER BECAUSE
THIS BIT IS COMPLETELY IGNORED!
#endif
Other changes in Swift 2.2…
++ and -- are deprecated | Changes in Swift

++ and -- are deprecated
Traditional C-style for loops are deprecated | Changes in Swift

Traditional C-style for loops are deprecated
Comparing tuples | Changes in Swift

Comparing tuples
Tuple splat syntax is deprecated | Changes in Swift

Tuple splat syntax is deprecated
More keywords can be used as argument labels | Changes in Swift

More keywords can be used as argument labels
Variable parameters have been deprecated | Changes in Swift

Variable parameters have been deprecated
Renamed debug identifiers: line, function, file | Changes in Swift

Renamed debug identifiers: line, function, file
Stringified selectors are deprecated | Changes in Swift

Stringified selectors are deprecated

Download Swift 2.2 playgroundopen in new window


이찬희 (MarkiiimarK)
Never Stop Learning.