控制流 / 条件语句 / if/else 语句

#if targetEnvironment(macCatalyst)
print("Launch on Mac")
#elseif targetEnvironment(simulator)
print("Launch on simulator")
#else
print("Launch on iOS")
#endif


Platform condition Valid arguments
os() macOS, iOS, watchOS, tvOS, visionOS, Linux, Windows
arch() i386, x86_64, arm, arm64
swift() >= or <  followed by a version number
compiler() >= or <  followed by a version number
canImport() A module name
targetEnvironment() simulator, macCatalyst

A conditional compilation block allows code to be conditionally compiled depending on the value of one or more compilation conditions.

Every conditional compilation block begins with the #if compilation directive and ends with the #endif compilation directive.