控制流
For-In Loops
遍历集合
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
遍历字典
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
遍历范围
1...5:表示从1到5并包含5.
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
1..<5:表示从1到5,但不包含5
for index in 1..<5 {
print("\(index) times 5 is \(index * 5)")
stride(from:to:by:)
from:起始值。to:最大值,生成的值小于该值。by:步长。
let minutes = 60
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}
stride(from:through:by:)
from:起始值。
through:最大值,生成的值小于等于该值。by:步长
let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
// render the tick mark every 3 hours (3, 6, 9, 12)
}
While循环
swift 提供了两种while循环
while
let count = 100;
var i = 0;
while i
repeat-while
类似于java的do-while,先执行代码,再判断条件是否满足
let count = 100;
var i = 0;
repeat{
i += 1;
print(i)
}while i
if
与java语法类似,条件为true时执行
let temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
print("It's really warm. Don't forget to wear sunscreen.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
Switch
case分支匹配后执行结束,case分支默认为break
let someCharacter: Character = "z"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
同时匹配两个分支:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
区间匹配
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
let naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
// Prints "There are dozens of moons orbiting Saturn.
自动匹配区间内的一个值。
Tuple 匹配
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (_, 0):
print("\(somePoint) is on the x-axis")
case (0, _):
print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
"" 表示匹配任何值,(,0)表示匹配有两个值的tuple,并且第一个是任何值,第二个值是0.
"-2...2"表示-2到2的整数,(-2...2, -2...2),表示匹配有两个值的tuple,并且第一个值是-2到2的整数,第二个值也是-2到2的整数。
值绑定
let anotherPoint = (2, 0)
switch anotherPoint {
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 a y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
}
case (let x, 0),匹配tuple的第二个值是0,并把tuple的第一个值赋值给x。
case (0, let y):匹配tuple的第一个值是0,并把tuple的第二个值赋值给x。
case let (x, y):匹配任何值的tuple,并把第一个值赋值给x,第二个值赋值给y
Where
类似SQL语句中的where , where后为条件。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
// Prints "(1, -1) is on the line x == -y
case let (x, y) where x == y:表示第一个值和第二值相等的tuple并把第一个值赋值给x,第二个值赋值给y。
case let (x, y) where x == -y:表示第一个值和第二值互为反数的tuple并把第一个值赋值给x,第二个值赋值给y。
复合条件
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a vowel or a consonant")
}
case "a", "e", "i", "o", "u":表示分支同时匹配"a", "e", "i", "o", "u"
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
print("On an axis, \(distance) from the origin")
default:
print("Not on an axis")
}
// Prints "On an axis, 9 from the origin"
case (let distance, 0), (0, let distance):表示同时匹配第一个值是0,或者第二个值是0的tuple,并且把另一个值赋值给distance。
控制跳转语句
continue
在循环中,停止执行continue之后的语句,进入下一个循环。
break
在循环中,停止执行break之后的语句,并跳出循环
在switch中, 不允许用空的分支,因此用break填充。
fallthrough
在switch中的分支默认是break的。使用fallthrough执行下一个分支。
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
print(description)
// Prints "The number 5 is a prime number, and also an integer."
标签语句
var i = 0
gameLoop: while i < 100 {
switch i {
case 0 ... 20:
i = i+3
var m = 0
while m 10){
print("continue gameLoop\(i),\(m)")
continue gameLoop
}
}
default:
var m = 0
while m 10){
print("break gameLoop\(i),\(m)")
break gameLoop
}
}
}
}
gameLoop是一个标签,一般用在内部循环,触发外部循环的跳转 。
break gameLoop,触发外部循环break。
continue gameLoop,触发外部循环continue。
提前退出
return,这在Java中也经常使用,满足某个条件后return , 不再继续执行后面的语句。
检测Api是否有效
if #available(iOS 10, macOS 10.12, *) {
// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
} else {
// Fall back to earlier iOS and macOS APIs
}