這篇文章主要講 swift 和 C 語言不同的地方
For-In Loops
Repeat-While
let c = 1
switch c {
case 0...9 : print("find 0~9")
case 5: print("find 5")
default:
print("not find")
}
5.switch 可以匹配一個元組
6.switch case 中要匹配的值可以是相同的
7.switch 支持值綁定
let c = 1
switch c {
case let x: print(x)
case 5: print("find 5")
default:
print("not find")
}
let c = 1
switch c {
case let x where x != 5 : print(x)
case 5: print("find 5")
default:
print("not find")
}
tag1: while true{
print("tag1")
tag2 : for(var i = 0 ; i <= 10; i++){
print("in for")
tag3:switch "4"{
case "c":
break tag1
default:
continue tag2
}
}
}
if #available(platform name version, ..., *) {
statements to execute if the APIs are available
} else {
fallback statements to execute if the APIs are unavailable
}
例子:
if #available(iOS 10, macOS 10.12, *) {
// 執行這裏 iOS最低版本爲iOS 10 macOS 最低版本爲 10.12
} else {
// Fall back to earlier iOS and macOS APIs
}