Swift5.0笔记 - 2. 流程控制
2020-10-21 本文已影响0人
南城同學
1. if-else
-
if
后面的条件可以省略小括号; - 条件后面的大括号不可以省略;
-
if
后面的条件只能是Bool
类型;
let age = 4
if age >= 22 {
}
2. while
-
repeat-while
相当于C语言中的do-while
var num = 5;
while num > 0 {
print("num is \(num)")
num -= 1
}//打印了5次
var num01 = 5;
repeat {
print("num is \(num01)")
} while num01 > 0 //打印了1次
- Swift3.0之后去掉了 num++、 num--这种运算形式;
3. for
3.1
-
闭区间运算符:
a...b
, a<= 取值 <= b
let name = ["Ann", "Toom", "Alex", "Jack"]
for i in 0...3 {
print(name[i])
}
//二
let range = 1...3
for i in range {
print(names[i])
}
//三
let a = 1
var b = 2
for i in a...b {
print(names[i])
}
//四
for i in a...3 {
print(names[i])
}
//i默认是let, 有需要时可以声明var
for var i in 1..3 {
}
//如果用不到“i”, 用“_”
for _ in 1...3 {
}
-
半开区间运算符:
a..<b
,a <= 取值 < b -
单侧区间:让区间朝一个方向近可能的远
let names = ["Ann", "Cbb", "Edd", "Gaa"]
//从角标“2”开始,到最大“3”结束
for name in names[2...] {
print(name)
}
//从最小开始,到“2”结束
for name in names[...2] {
print(name)
}
//从最小开始,到“1”结束
for name in names[..<2] {
print(name)
}
//数组之外的
//从负无穷,到5
let range = ...5
3.2 for
- 区间运算符用在数组上
let names = ["Ann", "Cbb", "Edd", "Gaa"]
for name in names[0...3] {
print(name)
}
4. 区间类型
4.1 不带间隔的区间类型
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...3
- 字符、字符串也能使用区间运算符,但默认不能用在
for-in
中
let stringRange1 = "cc"..."fff"
stringRange1.contains("cb") //false
stringRange1.contains("dz") //true
let stringRange2 = "a"..."f" //
stringRange2.contains("h") //false
stringRange2.contains("c") //true
-
"\0"..."~"
囊括了所有可能用到的ASCII字符
let characterRange3: ClosedRange<Character> = "\0"..."~"
4.2 带间隔的区间类型
let hours = 11
let hourInterval = 2
//tickMark的取值:从4开始,累加2,不超过11
for tickMark in stride(from: 4, though: hours, by: hourInterval) {
print(tickMark)
} // 4,6, 8, 10
5. switch
var num = 1
switch num {
case 1:
print("num is 1")
break
case 2:
print("num is 2")
break
default:
print("num is other")
break
} // 打印“1”
-
case
、default
后面不能写大括号{}; -
默认可以不写
break
,并不会贯穿后面的条件; -
case
、default
后面至少要有一条语句; -
必须保证能处理所有情况,略省的用
default
;
var num = 1
//这种会提示“switch must be exhaustive”
switch num {
case 1:
print("num is 1")
case 2:
print("num is 2")
}
- 如果能够保证已处理所有情况,也可以不必用
default
.
enum Answer {case right, wrong}
let answer = Answer.right
switch answer {
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
}
5.2 fallthough
- 使用
fallthough
可以实现贯穿效果
var num = 1
switch num {
case 1:
print("num is 1")
fallthough
case 2:
print("num is 2")
default:
print("num is other")
}
//打印 “1”、“2”
5.3 复合条件
-
switch
也支持Character
、String
类型
let string = "Jack"
switch string {
case "Jack":
fallthough
case "Rose":
print("Right person")
default:
break
}
switch string {
case "Jack", "Rose":
print("Right person")
default:
break
}
6. 区间匹配、元组匹配
let count = 30
switch count {
case 0:
print("none")
case 1..<5:
print("a few")
default:
break
}
let point = (1, 1)
switch point {
case (0, 0):
print("0, 0")
case(0,_):
print("on the y-axis")
case(-2...2, -2...2):
print("inside the box")
default:
break
}
7. 值绑定
- 只要其中一方符合,就可以
let point01 = (2, 0)
switch point01 {
case (let x, 0): // 条件满足,进入
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the x-axis with an x value of \(y)")
case let(x, y):
print("somewhere else at(\(x), \(y)) ")
}
打印:“on the x-axis with an x value of 2”
8. Where
- 附加加条件,用来过滤
let point02 = (1, -1)
switch point02 {
case let(x,y) where x == y:
print("on the line x == y")
case let(x,y) where x == -y: //当“x == -y”时走该条件
print("on the line x == y")
case let(x,y):
print("(\(x), \(y)) is just some arbitrary point ")
}
打印:“on the line x == y”
- 应用:将所有的整数加起来
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num0 in numbers where num0 > 0 {
sum += num0
}
print(sum)//60
9. 标签
outer: for m in 1...4 {
for k in 1...4 {
if k == 3 {
continue outer
}
if m == 3 {
break outer
}
print("m == \(m), k == \(k)")
}
}
打印:
m == 1, k == 1
m == 1, k == 2
m == 2, k == 1
m == 2, k == 2
- 结论:
-
continue
结束内部的本轮for循环 -
break
结束最外层的for循环
-