控制流
Swift包含很多控制流语句:while循环、if、guard、switch和for-in循环。
<br />
For-In循环
for-in循环用于迭代序列,如数组遍历、数字范围、字符串中的字符。
下面为for-in循环遍历数组:
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
遍历字典:
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs
// spiders have 8 legs
// cats have 4 legs
遍历数字范围:
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
若不需要序列中的值时,使用下划线代替变量名来忽略这些值:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
遍历半开数字范围:
let minutes = 60
for tickMark in 0..<minutes {
// render the tick mark each minute (60 times)
}
使用stride(from:to:by:)方法跳过不需要的遍历:
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:)方法遍历包括结尾值:
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)
}
<br />
While循环
Swift有两种while循环:
- 每次循环开始时判断循环条件的while循环
- 每次循环结束时判断循环条件的repeat-while循环
While
while循环语法:
while condition {
statements
}
Repeat-While
Swift的repeat-while循环与其他语言的do-while循环类似。
repeat-while循环语法:
repeat {
statements
} while condition
<br />
条件语句
Swift提供两种条件语句:if和switch语句。if判断简单条件,可能结果较少;switch适合复杂条件且可能情况较多的情况,常用于模式匹配。
If
Swift中的if语句可忽略判断条件的括号,但执行语句的大括号不可或缺:
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
}
// Prints "It's very cold. Consider wearing a scarf."
if-else语句:
temperatureInFahrenheit = 40
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
多重if语句:
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
switch语句比较多个匹配模式,且能匹配任意类型值,语法如下:
switch some value to consider {
case value 1:
respond to value 1
case value 2,
value 3:
respond to value 2 or 3
default:
otherwise, do something else
}
switch语句必须列出所有可能情况。
下面例子匹配单个小写字符:
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")
}
Swift的switch语句不需要在每个语句块最后显式使用break语句(也可使用)。当匹配某个条件时,之间结束执行整个switch语句。
switch语句的每个条件必须只要包含一个可执行语句,否则会抛出编译错误:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a": // Invalid, the case has an empty body
case "A":
print("The letter A")
default:
print("Not the letter A")
}
// This will report a compile-time error.
要将两个判断条件合并,使用逗号隔开:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
注意
要想显示跳过某次判断条件,进入下个条件,使用fallthrough关键字。
间隔匹配
使用间隔匹配可在一条判断语句中匹配多个值:
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."
匹配元组
使用元组可在一条switch语句中匹配复合值,并使用下划线匹配元组中某个元素的所有可能值。
当已经匹配某个条件时,之后的所有条件都将被忽略。
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")
}
// Prints "(1, 1) is inside the box"
值绑定
值绑定表示将匹配到的值命名为临时常量或变量。
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))")
}
// Prints "on the x-axis with an x value of 2"
当某个条件正好匹配所有可能的剩余情况时,则default语句可忽略。
Where
使用where语句对某个case添加额外条件:
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
使用逗号在一个case匹配多个条件,条件过长时可多行显示:
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")
}
// Prints "e is a vowel"
复合匹配中也可包含值绑定:
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"
<br />
控制转移语句
Swift有五种控制转移语句:
- continue
- break
- fallthrough
- return
- throw
Continue
continue语句表示在循环中跳过本次循环,直接开始下一次循环,而不结束循环:
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
if charactersToRemove.contains(character) {
continue
} else {
puzzleOutput.append(character)
}
}
print(puzzleOutput)
// Prints "grtmndsthnklk"
Break
break语句立即结束执行整个控制流语句。
用于循环语句的break
当在循环语句中使用break时,break会立即结束执行整个循环语句。
用于switch语句的break
当在switch语句中使用break时,break会立即结束执行整个switch。
Swift的switch语句中不用显示使用break跳出匹配,但可以在某个case中可以插入break结束匹配以使意图明确。
let numberSymbol: Character = "三" // Chinese symbol for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
possibleIntegerValue = 1
case "2", "٢", "二", "๒":
possibleIntegerValue = 2
case "3", "٣", "三", "๓":
possibleIntegerValue = 3
case "4", "٤", "四", "๔":
possibleIntegerValue = 4
default:
break
}
if let integerValue = possibleIntegerValue {
print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
print("An integer value could not be found for \(numberSymbol).")
}
// Prints "The integer value of 三 is 3."
Fallthrough
Swift中,switch语句在匹配到某个case条件后会忽略之后所有case,不会自动下一个case。若需要像C语言那样跳入下个case中,可使用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."
注意
fallthrough关键字不会检查下个进入的case判断条件,只是简单地跳入下一个case并执行内部代码,与C语言中的switch语句相似。
标签语句
当使用循环或条件嵌套语句时,需要明确说明要使用break或continue语句来结束哪层控制语句,这时使用标签语句对需要提前结束的控制语句做标签,然后在break或continue语句后接上已声明的标签名来结束带相同标签的语句。语法如下:
label name: while condition {
statements
}
<br />
guard语句
guard语句是if语句的变体,通过将判断条件的主体执行代码放在guard语句末尾以使代码意图更加清晰。
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello \(name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
greet(person: ["name": "John"])
// Prints "Hello John!"
// Prints "I hope the weather is nice near you."
greet(person: ["name": "Jane", "location": "Cupertino"])
// Prints "Hello Jane!"
// Prints "I hope the weather is nice in Cupertino."
<br />
检查API可用性
Swift支持检查API可用性,避免代码中使用不可用API导致意外错误。
当尝试使用不可用API时,Swift会报告编译错误。
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
}
语法如下:
if #available(platform name version, ..., *) {
statements to execute if the APIs are available
} else {
fallback statements to execute if the APIs are unavailable
}