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

if #available(iOS 12, macOS 12, *) {
    print("iOS 12, macOS 12 or higher")
else {
    print("iOS 11, macOS 11 or below")
}

if #unavailable(iOS 17) {
    print("iOS version 16 or below")
else {
    print("iOS version 17 or higher")
}


You use an availability condition in an if or guard statement to conditionally execute a block of code, depending on whether the APIs you want to use are available at runtime. The compiler uses the information from the availability condition when it verifies that the APIs in that block of code are available.