Build configuration import testing
About 2 min
Build configuration import testing 관련
HACKING WITH SWIFT
What's new in Swift?
Build configuration import testing | Changes in Swift 4.1
Build configuration import testing
Available from Swift 4.1
Swift 4.1 implemented SE-0075 (apple/swift-evolution
), which introduced a new canImport
condition that lets us check whether a specific module can be imported when our code is compiled.
This is particularly important for cross-platform code: if you had a Swift file that implemented one behavior on macOS and another on iOS, or if you needed specific functionality for Linux. For example:
#if canImport(SpriteKit)
// this will be true for iOS, macOS, tvOS, and watchOS
#else
// this will be true for other platforms, such as Linux
#endif
Previously you would have had to use inclusive or exclusive tests by operating system, like this:
#if !os(Linux)
// Matches macOS, iOS, watchOS, tvOS, and any other future platforms
#endif
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
// Matches only Apple platforms, but needs to be kept up to date as new platforms are added
#endif
The new canImport
condition lets us focus on the functionality we care about rather than what platform we're compiling for, thus avoiding a variety of problems.
Other Changes in Swift 4.1
Synthesized Equatable and Hashable | Changes in Swift 4.1
Synthesized Equatable and Hashable
Key decoding strategies for Codable | Changes in Swift 4.1
Key decoding strategies for Codable
Conditional conformances | Changes in Swift 4.1
Conditional conformances
Recursive constraints on associated types | Changes in Swift 4.1
Recursive constraints on associated types
Target environment testing | Changes in Swift 4.1
Target environment testing
flatMap is now (partly) compactMap() | Changes in Swift 4.1
flatMap is now (partly) compactMap()