1 for in
for in
可用来遍历 Array
Dictionary
Set
Range
String
1 | for index in 1...5 { |
2 while
1 | var i = 1 |
3 if
1 | var temperatureInFahrenheit = 30 |
4 switch
不存在隐式的贯穿(No Implicit Fallthrough),不需要break
1 | let anotherCharacter: Character = "a" |
区间匹配
1 | let approximateCount = 62 |
元组匹配
1 | let somePoint = (1, 1) |
值绑定(Value Bindings)
1 | let anotherPoint = (2, 0) |
case分句中使用where
1 | let yetAnotherPoint = (1, -1) |
复合匹配
当多个条件可以使用同一种方式来处理时,可以将这几种可能放在同一个case后面,并且用逗号隔开。
1 | let stillAnotherPoint = (9, 0) |
5 控制转移语句(Control Transfer Statements): continue break fallthrough return throw
贯穿(Fallthrough) 执行完一个case后,继续向下执行
1 | let integerToDescribe = 5 |
6 guard 与 if
功能类似。区别:1 guard减少嵌套,会简洁 2 解包时,guard的结果作用域不限于本身。
1 | // greet(person: ["name": "Jane", "location": "Cupertino"]) |
7 检测 API 可用性
1 | if #available(iOS 10, macOS 10.12, *) { |
playground文件在andyRon/LearnSwift