闭区间运算符:
- a...b , a<= 取值 <=b
let names = ["Tom","Jack","Tim","Bob"]
for i in 0...3 {
print(names[i])
}
let rangeCS = 1...3
for i in rangeCS {
print(names[i])
}
let aCS = 1
var bCS = 2
for i in aCS...bCS {
print(names[i])
}
for i in aCS...3 {
print(names[i])
}
- i默认是let,有需要时可以声明为var
for var i in 1...3 {
i += 5
print(i)
}
- 只需要循环次数时可以不声明i
for _ in 1...5 {
print("for")
}
- 半开区间运算符:a..<b, a <= 取值 <b
for i in 1..<5 {
print(i)
}
- 区间运算符用在数组上
let names = ["Tom","Jack","Tim","Bob"]
for name in names[0...3] {
print(name)
}
- 单侧区间:让区间朝一个方向尽可能远
for name in names[2...] {
print(name)
}
for name in names[...2] {
print(name)
}
for name in names[..<2] {
print(name)
}
- 区间 range
let rangeV = ...5
rangeV.contains(7) //false
rangeV.contains(4) //true
rangeV.contains(-3) //true
- 区间类型
// 闭区间
let range1: ClosedRange<Int> = 1...3
// 半开区间
let range2: Range<Int> = 1..<3
// 单侧区间
let range3:PartialRangeThrough<Int> = ...5
let range4:PartialRangeFrom<Int> = 1...
let range5:PartialRangeUpTo<Float> = ..<5.0
有兴趣更多类型可直接看Swift DefaultIndices 源码。
- 字符 与 字符串
let stringRange1 = "cc"..."ff"
stringRange1.contains("cb") //false
stringRange1.contains("dz") //true
stringRange1.contains("fg") //false
let stringRange2 = "a"..."f"
stringRange2.contains("d") //true
stringRange2.contains("h") // false
// \0 到 ~ 所有的ASKII码字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") //true
let str = "a"
let aCharacter: Character = "a"
let zCharacter: Character = "z"
let rangeCharacter = aCharacter...zCharacter
- 带间隔的区间值
let hours = 11
let hourInterval = 2
// tickMark 的取值 :从4开始,累加2,不超过11
for tickMark in stride(from: 4, to: hours, by: hourInterval) {
print(tickMark)
}
switch
- case 、default 后面不能写大括号{}
- 默认不写break,并不会贯穿到后面的条件
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
}
- fallthrough 贯穿
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
}
- switch 必须要保证能处理的所有情况
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
} //switch mush be exhaustive : switch 必须考虑所有情况
- case、default后面至少要有一条语句
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
}
//default' label in a 'switch' should have at least one executable statement
- 如果不想做任何事,加个break即可
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
break
}
- 如果能保证已处理所有情况,可以不必须使用default
enum AnswerCase {case right,wrong}
let ans = AnswerCase.right
switch ans {
case .right:
print("right")
case .wrong:
print("wrong")
}
- switch 符合条件
//支持 Character 、String 类型
let stringSwitch = "Jack"
switch stringSwitch {
case "Jack":
fallthrough
case "Rose":
print("Right person")
default:
break
}
- 区间匹配、元组匹配
- 可以使用_ 忽略某个值
- 关于case匹配问题,属于模式匹配(Pattern Matching)的范畴
// MARK: - 区间匹配、元组匹配
let count = 62
switch count {
case 0:
print("none")
case 1..<5:
print("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
}
let point = (1, 1)
switch point {
case (0, 0):
print("the origin")
case (_, 0):
print("on the x-axis")
case (0, _):
print("on the y-axis")
case (-2...2, -2...2):
print("inside the box")
default:
print("outside of the box")
}
- 值绑定
- 必要时let也可以改为var
let point1 = (2, 0)
switch point1 {
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with an y value of \(y)")
case (0, _):
print("on the y-axis")
case (-2...2, -2...2):
print("inside the box")
default:
print("outside of the box")
}
- where
- for循环中相当于continue
let point3 = (1,-1)
switch point3 {
case let (x, y) where x == y:
print("on the line x == y")
case let (x, y) where x == -y:
print("on the line x == -y")
case let (x, y) where x == y:
print("on the line x == y")
case let (x, y):
print("(\(x) ,\(y)) is just some arbitrary point")
}
//相当于 continue
var numbers1 = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers1 where num > 0 {
sum += num
}
print(sum)
- 标签语句 在内层循环跳出外循环
outer: for i in 1...4 {
for k in 1...4 {
if k == 3 {
continue outer
}
if i == 3 {
break outer
}
print("i === \(i), k === \(k)")
}
}
/* print
i === 1, k === 1
i === 1, k === 2
i === 2, k === 1
i === 2, k === 2
*/