Control Flow (控制流上)

Swift provides a variety of control flow statements. These includewhileloops to perform a task multiple times;if,guard, andswitchstatements to execute different branches of code based on certain conditions; and statements such asbreakandcontinueto transfer the flow of execution to another point in your code.

Swift提供了多种流程控制结构,包括可以多次执行任务的while循环,基于特定条件选择执行不同代码分支的if、guard和switch语句,还有控制流程跳转到其他代码位置的break和continue语句。

Swift also provides afor-inloop that makes it easy to iterate over arrays, dictionaries, ranges, strings, and other sequences.

Swift 还提供了for-in循环,用来更简单地遍历数组(array),字典(dictionary),区间(range),字符串(string)和其他序列类型。

Swift’sswitchstatement is also considerably more powerful than its counterpart in many C-like languages. Because the cases of aswitchstatement do not fall through to the next case in Swift, it avoids common C errors caused by missingbreakstatements. Cases can match many different patterns, including interval matches, tuples, and casts to a specific type. Matched values in aswitchcase can be bound to temporary constants or variables for use within the case’s body, and complex matching conditions can be expressed with awhereclause for each case.

Swift 的switch语句比 C 语言中更加强大。在 C 语言中,如果某个 case 不小心漏写了break,这个 case 就会贯穿至下一个 case,Swift 无需写break,所以不会发生这种贯穿的情况。case 还可以匹配很多不同的模式,包括间隔匹配(interval match),元组(tuple)和转换到特定类型。switch语句的 case 中匹配的值可以绑定成临时常量或变量,在case体内使用,也可以用where来描述更复杂的匹配条件。

For-In Loops (For-In循环)

You use thefor-inloop to iterate over a sequence, such as ranges of numbers, items in an array, or characters in a string.

你可以使用for-in循环来遍历一个集合中的所有元素,例如数字范围、数组中的元素或者字符串中的字符。

This example prints the first few entries in the five-times table:

下面的例子用来输出乘 5 乘法表前面一部分内容:

for in dexin1...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

The sequence being iterated over is a range of numbers from1to5, inclusive, as indicated by the use of the closed range operator (...). The value ofindexis set to the first number in the range (1), and the statements inside the loop are executed. In this case, the loop contains only one statement, which prints an entry from the five-times table for the current value ofindex. After the statement is executed, the value ofindexis updated to contain the second value in the range (2), and theprint(_:separator:terminator:)function is called again. This process continues until the end of the range is reached.

例子中用来进行遍历的元素是使用闭区间操作符(...)表示的从1到5的数字区间。index被赋值为闭区间中的第一个数字(1),然后循环中的语句被执行一次。在本例中,这个循环只包含一个语句,用来输出当前index值所对应的乘 5 乘法表的结果。该语句执行后,index的值被更新为闭区间中的第二个数字(2),之后print(_:separator:terminator:)函数会再执行一次。整个过程会进行到闭区间结尾为止。

In the example above,indexis a constant whose value is automatically set at the start of each iteration of the loop. As such,indexdoes not have to be declared before it is used. It is implicitly declared simply by its inclusion in the loop declaration, without the need for aletdeclaration keyword.

上面的例子中,index是一个每次循环遍历开始时被自动赋值的常量。这种情况下,index在使用前不需要声明,只需要将它包含在循环的声明中,就可以对其进行隐式声明,而无需使用let关键字声明。

If you don’t need each value from a sequence, you can ignore the values by using an underscore in place of a variable name.

如果你不需要区间序列内每一项的值,你可以使用下划线(_)替代变量名来忽略这个值:

let base=3

let power=10

var answer=1

for_in1...power{

    answer*=base

}

print("\(base)to the power of\(power)is\(answer)")

// Prints "3 to the power of 10 is 59049"

The example above calculates the value of one number to the power of another (in this case,3to the power of10). It multiplies a starting value of1(that is,3to the power of0) by3, ten times, using a closed range that starts with1and ends with10. For this calculation, the individual counter values each time through the loop are unnecessary—the code simply executes the loop the correct number of times. The underscore character (_) used in place of a loop variable causes the individual values to be ignored and does not provide access to the current value during each iteration of the loop.

这个例子计算 base 这个数的 power 次幂(本例中,是3的10次幂),从1(3的0次幂)开始做3的乘法, 进行10次,使用1到10的闭区间循环。这个计算并不需要知道每一次循环中计数器具体的值,只需要执行了正确的循环次数即可。下划线符号_(替代循环中的变量)能够忽略当前值,并且不提供循环遍历时对值的访问。

Use afor-inloop with an array to iterate over its items.

使用for-in遍历一个数组所有元素:

let names= ["Anna","Alex","Brian","Jack"]

for name in names{

    print("Hello,\(name)!")

}

// Hello, Anna!

// Hello, Alex!

// Hello, Brian!

// Hello, Jack!

You can also iterate over a dictionary to access its key-value pairs. Each item in the dictionary is returned as a(key, value)tuple when the dictionary is iterated, and you can decompose the(key, value)tuple’s members as explicitly named constants for use within the body of thefor-inloop. Here, the dictionary’s keys are decomposed into a constant calledanimalName, and the dictionary’s values are decomposed into a constant calledlegCount.

你也可以通过遍历一个字典来访问它的键值对。遍历字典时,字典的每项元素会以(key, value)元组的形式返回,你可以在for-in循环中使用显式的常量名称来解读(key, value)元组。下面的例子中,字典的键(key)解读为常量animalName,字典的值会被解读为常量legCount:

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

Items in aDictionarymay not necessarily be iterated in the same order in which they were inserted. The contents of aDictionaryare inherently unordered, and iterating over them does not guarantee the order in which they will be retrieved. For more on arrays and dictionaries, seeCollection Types.

字典元素的遍历顺序和插入顺序可能不同,字典的内容在内部是无序的,所以遍历元素时不能保证顺序。关于数组和字典,详情参见集合类型

While Loops (While循环)

Awhileloop performs a set of statements until a condition becomesfalse. These kinds of loops are best used when the number of iterations is not known before the first iteration begins. Swift provides two kinds ofwhileloops:

while循环会一直运行一段语句直到条件变成false。这类循环适合使用在第一次迭代前,迭代次数未知的情况下。Swift 提供两种while循环形式:

while evaluates its condition at the start of each pass through the loop.

while循环,每次在循环开始时计算条件是否符合;

repeat-whileevaluates its condition at the end of each pass through the loop.

repeat-while循环,每次在循环结束时计算条件是否符合。

While (While)

Awhileloop starts by evaluating a single condition. If the condition istrue, a set of statements is repeated until the condition becomesfalse.

while循环从计算一个条件开始。如果条件为true,会重复运行一段语句,直到条件变为false。

Here’s the general form of awhileloop:

下面是while循环的一般格式:

while condition{

    statements

}

This example plays a simple game ofSnakes and Ladders(also known asChutes and Ladders):

下面的例子来玩一个叫做蛇和梯子(也叫做滑道和梯子)的小游戏:

The rules of the game are as follows:

游戏的规则如下:

1. The board has 25 squares, and the aim is to land on or beyond square 25.

游戏盘面包括 25 个方格,游戏目标是达到或者超过第 25 个方格;

2. Each turn, you roll a six-sided dice and move by that number of

squares, following the horizontal path indicated by the dotted arrow

above.

每一轮,你通过掷一个六面体骰子来确定你移动方块的步数,移动的路线由上图中横向的虚线所示;

3. If your turn ends at the bottom of a ladder, you move up that ladder.

如果在某轮结束,你移动到了梯子的底部,可以顺着梯子爬上去;

4. If your turn ends at the head of a snake, you move down that snake.

如果在某轮结束,你移动到了蛇的头部,你会顺着蛇的身体滑下去。

The game board is represented by an array ofIntvalues. Its size is based on a constant calledfinalSquare, which is used to initialize the array and also to check for a win condition later in the example. The board is initialized with 26 zeroIntvalues, not 25 (one each at indexes0through25).

游戏盘面可以使用一个Int数组来表达。数组的长度由一个finalSquare常量储存,用来初始化数组和检测最终胜利条件。游戏盘面由 26 个Int0 值初始化,而不是 25 个(由0到25,一共 26 个):

let finalSquare=25

varboard= [Int](repeating:0,count:finalSquare+1)

Some squares are then set to have more specific values for the snakes and ladders. Squares with a ladder base have a positive number to move you up the board, whereas squares with a snake head have a negative number to move you back down the board.

一些方格被设置成特定的值来表示有蛇或者梯子。梯子底部的方格是一个正值,使你可以向上移动,蛇头处的方格是一个负值,会让你向下移动:

board[03] = +08;board[06] = +11;board[09] = +09;board[10] = +02

board[14] =-10;board[19] =-11;board[22] =-02;board[24] =-08

Square 3 contains the bottom of a ladder that moves you up to square 11. To represent this,board[03]is equal to+08, which is equivalent to an integer value of8(the difference between3and11). The unary plus operator (+i) balances with the unary minus operator (-i), and numbers lower than10are padded with zeros so that all board definitions align. (Neither stylistic tweak is strictly necessary, but they lead to neater code.)

3 号方格是梯子的底部,会让你向上移动到 11 号方格,我们使用board[03]等于+08(来表示11和3之间的差值)。使用一元正运算符(+i)是为了和一元负运算符(-i)对称,为了让盘面代码整齐,小于 10 的数字都使用 0 补齐(这些风格上的调整都不是必须的,只是为了让代码看起来更加整洁)。

The player’s starting square is “square zero”, which is just off the bottom-left corner of the board. The first dice roll always moves the player onto the board.

玩家由左下角空白处编号为 0 的方格开始游戏。玩家第一次掷骰子后才会进入游戏盘面:

var square=0

var diceRoll=0

while square

// roll the dice

diceRoll+=1

if diceRoll==7 {diceRoll=1}

// move by the rolled amount

square+=diceRoll

if square

// if we're still on the board, move up or down for a snake or a ladder

// 如果玩家还在棋盘上,顺着梯子爬上去或者顺着蛇滑下去

square+=board[square]

}

}

print("Game over!")

The example above uses a very simple approach to dice rolling. Instead of generating a random number, it starts with adiceRollvalue of0. Each time through thewhileloop,diceRollis incremented by one and is then checked to see whether it has become too large. Whenever this return value equals7, the dice roll has become too large and is reset to a value of1. The result is a sequence ofdiceRollvalues that is always1,2,3,4,5,6,1,2and so on.

本例中使用了最简单的方法来模拟掷骰子。diceRoll的值并不是一个随机数,而是以0为初始值,之后每一次while循环,diceRoll的值增加 1 ,然后检测是否超出了最大值。当diceRoll的值等于 7 时,就超过了骰子的最大值,会被重置为1。所以diceRoll的取值顺序会一直是1,2,3,4,5,6,1,2等。

After rolling the dice, the player moves forward bydiceRollsquares. It’s possible that the dice roll may have moved the player beyond square 25, in which case the game is over. To cope with this scenario, the code checks thatsquareis less than theboardarray’scountproperty before adding the value stored inboard[square]onto the currentsquarevalue to move the player up or down any ladders or snakes.

掷完骰子后,玩家向前移动diceRoll个方格,如果玩家移动超过了第 25 个方格,这个时候游戏将会结束,为了应对这种情况,代码会首先判断square的值是否小于board的count属性,只有小于才会在board[square]上增加square,来向前或向后移动(遇到了梯子或者蛇)。

Note

Had this check not been performed,board[square]might try to access a value outside the bounds of theboardarray, which would trigger an error. Ifsquarewere equal to26, the code would try to check the value ofboard[26], which is larger than the size of the array.

如果没有这个检测(square < board.count),board[square]可能会越界访问board数组,导致错误。如果square等于26, 代码会去尝试访问board[26],超过数组的长度。

The currentwhileloop execution then ends, and the loop’s condition is checked to see if the loop should be executed again. If the player has moved on or beyond square number25, the loop’s condition evaluates tofalseand the game ends.

当本轮while循环运行完毕,会再检测循环条件是否需要再运行一次循环。如果玩家移动到或者超过第 25 个方格,循环条件结果为false,此时游戏结束。

Awhileloop is appropriate in this case, because the length of the game is not clear at the start of thewhileloop. Instead, the loop is executed until a particular condition is satisfied.

while循环比较适合本例中的这种情况,因为在while循环开始时,我们并不知道游戏要跑多久,只有在达成指定条件时循环才会结束。

Repeat-While

The other variation of thewhileloop, known as therepeat-whileloop, performs a single pass through the loop block first,beforeconsidering the loop’s condition. It then continues to repeat the loop until the condition isfalse.

while循环的另外一种形式是repeat-while,它和while的区别是在判断循环条件之前,先执行一次循环的代码块。然后重复循环直到条件为false。

Note

Therepeat-whileloop in Swift is analogous to ado-whileloop in other languages.

Swift语言的repeat-while循环和其他语言中的do-while循环是类似的。

Here’s the general form of arepeat-whileloop:

下面是repeat-while循环的一般格式:

repeat{

    statements

}while condition

Here’s theSnakes and Laddersexample again, written as arepeat-whileloop rather than awhileloop. The values offinalSquare,board,square, anddiceRollare initialized in exactly the same way as with awhileloop.

还是蛇和梯子的游戏,使用repeat-while循环来替代while循环。finalSquare、board、square和diceRoll的值初始化同while循环时一样:

let finalSquare=25

var board= [Int](repeating:0,count:finalSquare+1)

board[03] = +08;board[06] = +11;board[09] = +09;board[10] = +02

board[14] =-10;board[19] =-11;board[22] =-02;board[24] =-08

var square=0

var diceRoll=0

In this version of the game, thefirstaction in the loop is to check for a ladder or a snake. No ladder on the board takes the player straight to square 25, and so it isn’t possible to win the game by moving up a ladder. Therefore, it’s safe to check for a snake or a ladder as the first action in the loop.

repeat-while的循环版本,循环中第一步就需要去检测是否在梯子或者蛇的方块上。没有梯子会让玩家直接上到第 25 个方格,所以玩家不会通过梯子直接赢得游戏。这样在循环开始时先检测是否踩在梯子或者蛇上是安全的。

At the start of the game, the player is on “square zero”.board[0]always equals0and has no effect.

游戏开始时,玩家在第 0 个方格上,board[0]一直等于 0, 不会有什么影响:

repeat{

// move up or down for a snake or ladder

// 顺着梯子爬上去或者顺着蛇滑下去

square+=board[square]

// roll the dice

diceRoll+=1

if diceRoll==7{diceRoll=1}

// move by the rolled amount

// 根据点数移动

square+=diceRoll

}while square

print("Game over!")

After the code checks for snakes and ladders, the dice is rolled and the player is moved forward bydiceRollsquares. The current loop execution then ends.

检测完玩家是否踩在梯子或者蛇上之后,开始掷骰子,然后玩家向前移动diceRoll个方格,本轮循环结束。

The loop’s condition (while square < finalSquare) is the same as before, but this time it’s not evaluated until theendof the first run through the loop. The structure of therepeat-whileloop is better suited to this game than thewhileloop in the previous example. In therepeat-whileloop above,square += board[square]is always executedimmediately afterthe loop’swhilecondition confirms thatsquareis still on the board. This behavior removes the need for the array bounds check seen in the earlier version of the game.

循环条件(while square < finalSquare)和while方式相同,但是只会在循环结束后进行计算。在这个游戏中,repeat-while表现得比while循环更好。repeat-while方式会在条件判断square没有超出后直接运行square += board[square],这种方式可以去掉while版本中的数组越界判断。

Conditional Statements (条件语句)

It is often useful to execute different pieces of code based on certain conditions. You might want to run an extra piece of code when an error occurs, or to display a message when a value becomes too high or too low. To do this, you make parts of your codeconditional.

根据特定的条件执行特定的代码通常是十分有用的。当错误发生时,你可能想运行额外的代码;或者,当值太大或太小时,向用户显示一条消息。要实现这些功能,你就需要使用条件语句

Swift provides two ways to add conditional branches to your code: theifstatement and theswitchstatement. Typically, you use theifstatement to evaluate simple conditions with only a few possible outcomes. Theswitchstatement is better suited to more complex conditions with multiple possible permutations and is useful in situations where pattern matching can help select an appropriate code branch to execute.

Swift 提供两种类型的条件语句:if语句和switch语句。通常,当条件较为简单且可能的情况很少时,使用if语句。而switch语句更适用于条件较复杂、有更多排列组合的时候。并且switch在需要用到模式匹配(pattern-matching)的情况下会更有用。

If (语句)

In its simplest form, theifstatement has a singleifcondition. It executes a set of statements only if that condition istrue.

if语句最简单的形式就是只包含一个条件,只有该条件为true时,才执行相关代码:

var temperatureInFahrenheit=30

if temperatureInFahrenheit<=32{

   print("It's very cold. Consider wearing a scarf.")

}

// Prints "It's very cold. Consider wearing a scarf."

The example above checks whether the temperature is less than or equal to 32 degrees Fahrenheit (the freezing point of water). If it is, a message is printed. Otherwise, no message is printed, and code execution continues after theifstatement’s closing brace.

上面的例子会判断温度是否小于等于 32 华氏度(水的冰点)。如果是,则打印一条消息;否则,不打印任何消息,继续执行if块后面的代码。

Theifstatement can provide an alternative set of statements, known as anelse clause, for situations when theifcondition isfalse. These statements are indicated by theelsekeyword.

当然,if语句允许二选一执行,叫做else从句。也就是当条件为false时,执行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.")

}

// Prints "It's not that cold. Wear a t-shirt."

One of these two branches is always executed. Because the temperature has increased to40degrees Fahrenheit, it is no longer cold enough to advise wearing a scarf and so theelsebranch is triggered instead.

显然,这两条分支中总有一条会被执行。由于温度已升至 40 华氏度,不算太冷,没必要再围围巾。因此,else分支就被触发了。

You can chain multipleifstatements together to consider additional clauses.

你可以把多个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.")

}

// Prints "It's really warm. Don't forget to wear sunscreen."

Here, an additionalifstatement was added to respond to particularly warm temperatures. The finalelseclause remains, and it prints a response for any temperatures that are neither too warm nor too cold.

在上面的例子中,额外的if语句用于判断是不是特别热。而最后的else语句被保留了下来,用于打印既不冷也不热时的消息。

The finalelseclause is optional, however, and can be excluded if the set of conditions does not need to be complete.

实际上,当不需要完整判断情况的时候,最后的else语句是可选的:

temperatureInFahrenheit=72

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.")

}

Because the temperature is neither too cold nor too warm to trigger theiforelse ifconditions, no message is printed.

在这个例子中,由于既不冷也不热,所以不会触发if或else if分支,也就不会打印任何消息。

Switch

Aswitchstatement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first pattern that matches successfully. Aswitchstatement provides an alternative to theifstatement for responding to multiple potential states.

switch语句会尝试把某个值与若干个模式(pattern)进行匹配。根据第一个匹配成功的模式,switch语句会执行对应的代码。当有可能的情况较多时,通常用switch语句替换if语句。

In its simplest form, aswitchstatement compares a value against one or more values of the same type.

switch语句最简单的形式就是把某个值与一个或若干个相同类型的值作比较:

switch some value to consider{

  case value 1:

      respond to value 1

 casevalue 2,

 value 3:

      respond to value 2 or 3

default:

     otherwise, do something else

}

Everyswitchstatement consists of multiple possiblecases, each of which begins with thecasekeyword. In addition to comparing against specific values, Swift provides several ways for each case to specify more complex matching patterns. These options are described later in this chapter.

switch语句由多个 case构成,每个由case关键字开始。为了匹配某些更特定的值,Swift 提供了几种方法来进行更复杂的模式匹配,这些模式将在本节的稍后部分提到。

Like the body of anifstatement, eachcaseis a separate branch of code execution. Theswitchstatement determines which branch should be selected. This procedure is known asswitchingon the value that is being considered.

与if语句类似,每一个 case 都是代码执行的一条分支。switch语句会决定哪一条分支应该被执行,这个流程被称作根据给定的值切换(switching)

Everyswitchstatement must beexhaustive. That is, every possible value of the type being considered must be matched by one of theswitchcases. If it’s not appropriate to provide a case for every possible value, you can define a default case to cover any values that are not addressed explicitly. This default case is indicated by thedefaultkeyword, and must always appear last.

switch语句必须是完备的。这就是说,每一个可能的值都必须至少有一个 case 分支与之对应。在某些不可能涵盖所有值的情况下,你可以使用默认(default)分支来涵盖其它所有没有对应的值,这个默认分支必须在switch语句的最后面。

This example uses aswitchstatement to consider a single lowercase character calledsomeCharacter:

下面的例子使用switch语句来匹配一个名为someCharacter的小写字符:

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")

}

// Prints "The last letter of the alphabet"

Theswitchstatement’s first case matches the first letter of the English alphabet,a, and its second case matches the last letter,z. Because theswitchmust have a case for every possible character, not just every alphabetic character, thisswitchstatement uses adefaultcase to match all characters other thanaandz. This provision ensures that theswitchstatement is exhaustive.

在这个例子中,第一个 case 分支用于匹配第一个英文字母a,第二个 case 分支用于匹配最后一个字母z。因为switch语句必须有一个case分支用于覆盖所有可能的字符,而不仅仅是所有的英文字母,所以switch语句使用default分支来匹配除了a和z外的所有值,这个分支保证了swith语句的完备性。

No Implicit Fallthrough

不存在隐式的贯穿

In contrast withswitchstatements in C and Objective-C,switchstatements in Swift do not fall through the bottom of each case and into the next one by default. Instead, the entireswitchstatement finishes its execution as soon as the first matchingswitchcase is completed, without requiring an explicitbreakstatement. This makes theswitchstatement safer and easier to use than the one in C and avoids executing more than oneswitchcase by mistake.

与 C 和 Objective-C 中的switch语句不同,在 Swift 中,当匹配的 case 分支中的代码执行完毕后,程序会终止switch语句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用break语句。这使得switch语句更安全、更易用,也避免了因忘记写break语句而产生的错误。

Note

Althoughbreakis not required in Swift, you can use abreakstatement to match and ignore a particular case or to break out of a matched case before that case has completed its execution. For details, seeBreak in a Switch Statement.

虽然在Swift中break不是必须的,但你依然可以在 case 分支中的代码执行完毕前使用break跳出,详情请参见Switch 语句中的 break

The body of each casemustcontain at least one executable statement. It is not valid to write the following code, because the first case is empty:

每一个 case 分支都必须包含至少一条语句。像下面这样书写代码是无效的,因为第一个 case 分支是空的:

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.

Unlike aswitchstatement in C, thisswitchstatement does not match both"a"and"A". Rather, it reports a compile-time error thatcase "a":does not contain any executable statements. This approach avoids accidental fallthrough from one case to another and makes for safer code that is clearer in its intent.

不像 C 语言里的switch语句,在 Swift 中,switch语句不会一起匹配"a"和"A"。相反的,上面的代码会引起编译期错误:case "a": 不包含任何可执行语句——这就避免了意外地从一个 case 分支贯穿到另外一个,使得代码更安全、也更直观。

To make aswitchwith a single case that matches both"a"and"A", combine the two values into a compound case, separating the values with commas.

为了让单个case同时匹配a和A,可以将这个两个值组合成一个复合匹配,并且用逗号分开:

let anotherCharacter:Character="a"

switch anotherCharacter{

case"a","A":

   print("The letter A")

default:

   print("Not the letter A")

}

// Prints "The letter A"

For readability, a compound case can also be written over multiple lines. For more information about compound cases, seeCompound Cases.

为了可读性,符合匹配可以写成多行形式,详情请参考复合匹配

Note

To explicitly fall through at the end of a particularswitchcase, use thefallthroughkeyword, as described inFallthrough.

如果想要显式贯穿case分支,请使用fallthrough语句,详情请参考贯穿

Interval Matching (区间匹配)

Values inswitchcases can be checked for their inclusion in an interval. This example uses number intervals to provide a natural-language count for numbers of any size:

case 分支的模式也可以是一个值的区间。下面的例子展示了如何使用区间匹配来输出任意数字对应的自然语言格式:

let approximateCount=62

let countedThings="moons orbiting Saturn"

var naturalCount:String

switch approximateCount{

  case0:

    naturalCount="no"

  case1..<5:

    naturalCount="a few"

  case5..<12:

    naturalCount="several"

  case12..<100:

    naturalCount="dozens of"

  case100..<1000:

    naturalCount="hundreds of"

  default:

    naturalCount="many"

}

print("There are\(naturalCount)\(countedThings).")

// Prints "There are dozens of moons orbiting Saturn."

In the above example,approximateCountis evaluated in aswitchstatement. Eachcasecompares that value to a number or interval. Because the value ofapproximateCountfalls between 12 and 100,naturalCountis assigned the value"dozens of", and execution is transferred out of theswitchstatement.

在上例中,approximateCount在一个switch声明中被评估。每一个case都与之进行比较。因为approximateCount落在了 12 到 100 的区间,所以naturalCount等于"dozens of"值,并且此后的执行跳出了switch语句。

Tuples (元组)

You can use tuples to test multiple values in the sameswitchstatement. Each element of the tuple can be tested against a different value or interval of values. Alternatively, use the underscore character (_), also known as the wildcard pattern, to match any possible value.

我们可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。

The example below takes an (x, y) point, expressed as a simple tuple of type(Int, Int), and categorizes it on the graph that follows the example.

下面的例子展示了如何使用一个(Int, Int)类型的元组来分类下图中的点(x, y):

let somePoint= (1,1)

switch somePoint{

case(0,0):

  print("(0, 0) is at the origin")

case(_,0):

  print("(\(somePoint.0), 0) is on the x-axis")

case(0,_):

  print("(0,\(somePoint.1)) is on the y-axis")

case(-2...2,-2...2):

  print("(\(somePoint.0),\(somePoint.1)) is inside the box")

default:

  print("(\(somePoint.0),\(somePoint.1)) is outside of the box")

}

// Prints "(1, 1) is inside the box"


Theswitchstatement determines whether the point is at the origin (0, 0), on the red x-axis, on the orange y-axis, inside the blue 4-by-4 box centered on the origin, or outside of the box.

在上面的例子中,switch语句会判断某个点是否是原点(0, 0),是否在红色的x轴上,是否在橘黄色的y轴上,是否在一个以原点为中心的4x4的蓝色矩形里,或者在这个矩形外面。

Unlike C, Swift allows multipleswitchcases to consider the same value or values. In fact, the point (0, 0) could match allfourof the cases in this example. However, if multiple matches are possible, the first matching case is always used. The point (0, 0) would matchcase (0, 0)first, and so all other matching cases would be ignored.

不像 C 语言,Swift 允许多个 case 匹配同一个值。实际上,在这个例子中,点(0, 0)可以匹配所有四个 case。但是,如果存在多个匹配,那么只会执行第一个被匹配到的 case 分支。考虑点(0, 0)会首先匹配case (0, 0),因此剩下的能够匹配的分支都会被忽视掉。

Value Bindings (值绑定)

Aswitchcase can bind the value or values it matches to temporary constants or variables, for use in the body of the case. This behavior is known asvalue binding, because the values are bound to temporary constants or variables within the case’s body.

case 分支允许将匹配的值绑定到一个临时的常量或变量,并且在case分支体内使用 —— 这种行为被称为值绑定(value binding),因为匹配的值在case分支体内,与临时的常量或变量绑定。

The example below takes an (x, y) point, expressed as a tuple of type(Int, Int), and categorizes it on the graph that follows:

下面的例子展示了如何在一个(Int, Int)类型的元组中使用值绑定来分类下图中的点(x, y):

let anotherPoint= (2,0)

switch anotherPoint{

  case(letx,0):

     print("on the x-axis with an x value of\(x)")

  case(0,lety):

     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"


Theswitchstatement determines whether the point is on the red x-axis, on the orange y-axis, or elsewhere (on neither axis).

在上面的例子中,switch语句会判断某个点是否在红色的x轴上,是否在橘黄色的y轴上,或者不在坐标轴上。

The threeswitchcases declare placeholder constantsxandy, which temporarily take on one or both tuple values fromanotherPoint. The first case,case (let x, 0), matches any point with ayvalue of0and assigns the point’sxvalue to the temporary constantx. Similarly, the second case,case (0, let y), matches any point with anxvalue of0and assigns the point’syvalue to the temporary constanty.

这三个 case 都声明了常量x和y的占位符,用于临时获取元组anotherPoint的一个或两个值。第一个 case ——case (let x, 0)将匹配一个纵坐标为0的点,并把这个点的横坐标赋给临时的常量x。类似的,第二个 case ——case (0, let y)将匹配一个横坐标为0的点,并把这个点的纵坐标赋给临时的常量y。

After the temporary constants are declared, they can be used within the case’s code block. Here, they are used to print the categorization of the point.

一旦声明了这些临时的常量,它们就可以在其对应的 case 分支里使用。在这个例子中,它们用于打印给定点的类型。

Thisswitchstatement does not have adefaultcase. The final case,case let (x, y), declares a tuple of two placeholder constants that can match any value. BecauseanotherPointis always a tuple of two values, this case matches all possible remaining values, and adefaultcase is not needed to make theswitchstatement exhaustive.

请注意,这个switch语句不包含默认分支。这是因为最后一个 case ——case let(x, y)声明了一个可以匹配余下所有值的元组。这使得switch语句已经完备了,因此不需要再书写默认分支。

Where

Aswitchcase can use awhereclause to check for additional conditions.

case 分支的模式可以使用where语句来判断额外的条件。

The example below categorizes an (x, y) point on the following graph:

下面的例子把下图中的点(x, y)进行了分类:

let yetAnotherPoint= (1,-1)

switch yetAnotherPoint{

case let(x,y)wherex==y:

    print("(\(x),\(y)) is on the line x == y")

case let(x,y)wherex== -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"

Theswitchstatement determines whether the point is on the green diagonal line wherex == y, on the purple diagonal line wherex == -y, or neither.

在上面的例子中,switch语句会判断某个点是否在绿色的对角线x == y上,是否在紫色的对角线x == -y上,或者不在对角线上。

The threeswitchcases declare placeholder constantsxandy, which temporarily take on the two tuple values fromyetAnotherPoint. These constants are used as part of awhereclause, to create a dynamic filter. Theswitchcase matches the current value ofpointonly if thewhereclause’s condition evaluates totruefor that value.

这三个 case 都声明了常量x和y的占位符,用于临时获取元组yetAnotherPoint的两个值。这两个常量被用作where语句的一部分,从而创建一个动态的过滤器(filter)。当且仅当where语句的条件为true时,匹配到的 case 分支才会被执行。

As in the previous example, the final case matches all possible remaining values, and so adefaultcase is not needed to make theswitchstatement exhaustive.

就像是值绑定中的例子,由于最后一个 case 分支匹配了余下所有可能的值,switch语句就已经完备了,因此不需要再书写默认分支。

Compound Cases (复合匹配)

Multiple switch cases that share the same body can be combined by writing several patterns aftercase, with a comma between each of the patterns. If any of the patterns match, then the case is considered to match. The patterns can be written over multiple lines if the list is long. For example:

当多个条件可以使用同一种方法来处理时,可以将这几种可能放在同一个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"

Theswitchstatement’s first case matches all five lowercase vowels in the English language. Similarly, its second case matches all lowercase English consonants. Finally, thedefaultcase matches any other character.

这个switch语句中的第一个case,匹配了英语中的五个小写元音字母。相似的,第二个case匹配了英语中所有的小写辅音字母。最终,default分支匹配了其它所有字符。

Compound cases can also include value bindings. All of the patterns of a compound case have to include the same set of value bindings, and each binding has to get a value of the same type from all of the patterns in the compound case. This ensures that, no matter which part of the compound case matched, the code in the body of the case can always access a value for the bindings and that the value always has the same type.

复合匹配同样可以包含值绑定。复合匹配里所有的匹配模式,都必须包含相同的值绑定。并且每一个绑定都必须获取到相同类型的值。这保证了,无论复合匹配中的哪个模式发生了匹配,分支体内的代码,都能获取到绑定的值,并且绑定的值都有一样的类型。

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"

Thecaseabove has two patterns:(let distance, 0)matches points on the x-axis and(0, let distance)matches points on the y-axis. Both patterns include a binding fordistanceanddistanceis an integer in both patterns—which means that the code in the body of thecasecan always access a value fordistance.

上面的case有两个模式:(let distance, 0)匹配了在x轴上的值,(0, let distance)匹配了在y轴上的值。两个模式都绑定了distance,并且distance在两种模式下,都是整型——这意味着分支体内的代码,只要case匹配,都可以获取到distance值

Control Transfer Statements (控制转移语句)

Control transfer statementschange the order in which your code is executed, by transferring control from one piece of code to another. Swift has five control transfer statements:

控制转移语句改变你代码的执行顺序,通过它可以实现代码的跳转。Swift 有五种控制转移语句:

continue

break

fallthrough

return

throw

Thecontinue,break, andfallthroughstatements are described below. Thereturnstatement is described inFunctions, and thethrowstatement is described inPropagating Errors Using Throwing Functions.

我们将会在下面讨论continue、break和fallthrough语句。return语句将会在函数章节讨论,throw语句会在错误抛出章节讨论。

Continue

Thecontinuestatement tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop. It says “I am done with the current loop iteration” without leaving the loop altogether.

continue语句告诉一个循环体立刻停止本次循环,重新开始下次循环。就好像在说“本次循环我已经执行完了”,但是并不会离开整个循环体。

The following example removes all vowels and spaces from a lowercase string to create a cryptic puzzle phrase:

下面的例子把一个小写字符串中的元音字母和空格字符移除,生成了一个含义模糊的短句:

let puzzleInput="great minds think alike"

var puzzleOutput=""

let charactersToRemove: [Character] = ["a","e","i","o","u"," "]

for character in puzzleInput.characters{

if charactersToRemove.contains(character) {

    continue

} else {

    puzzleOutput.append(character)

}

}

print(puzzleOutput)

// Prints "grtmndsthnklk"

The code above calls thecontinuekeyword whenever it matches a vowel or a space, causing the current iteration of the loop to end immediately and to jump straight to the start of the next iteration.

在上面的代码中,只要匹配到元音字母或者空格字符,就调用continue语句,使本次循环结束,重新开始下次循环。这种行为使switch匹配到元音字母和空格字符时不做处理,而不是让每一个匹配到的字符都被打印。

Break

Thebreakstatement ends execution of an entire control flow statement immediately. Thebreakstatement can be used inside aswitchstatement or loop statement when you want to terminate the execution of theswitchor loop statement earlier than would otherwise be the case.

break语句会立刻结束整个控制流的执行。当你想要更早的结束一个switch代码块或者一个循环体时,你都可以使用break语句。

Break in a Loop Statement (循环语句中的break)

When used inside a loop statement,breakends the loop’s execution immediately and transfers control to the code after the loop’s closing brace (}). No further code from the current iteration of the loop is executed, and no further iterations of the loop are started.

当在一个循环体中使用break时,会立刻中断该循环体的执行,然后跳转到表示循环体结束的大括号(})后的第一行代码。不会再有本次循环的代码被执行,也不会再有下次的循环产生。

Break in a Switch Statement (switch语句中的break)

When used inside aswitchstatement,breakcauses theswitchstatement to end its execution immediately and to transfer control to the code after theswitchstatement’s closing brace (}).

当在一个switch代码块中使用break时,会立即中断该switch代码块的执行,并且跳转到表示switch代码块结束的大括号(})后的第一行代码。

This behavior can be used to match and ignore one or more cases in aswitchstatement. Because Swift’sswitchstatement is exhaustive and does not allow empty cases, it is sometimes necessary to deliberately match and ignore a case in order to make your intentions explicit. You do this by writing thebreakstatement as the entire body of the case you want to ignore. When that case is matched by theswitchstatement, thebreakstatement inside the case ends theswitchstatement’s execution immediately.

这种特性可以被用来匹配或者忽略一个或多个分支。因为 Swift 的switch需要包含所有的分支而且不允许有为空的分支,有时为了使你的意图更明显,需要特意匹配或者忽略某个分支。那么当你想忽略某个分支时,可以在该分支内写上break语句。当那个分支被匹配到时,分支内的break语句立即结束switch代码块。

Note

Aswitchcase that contains only a comment is reported as a compile-time error. Comments are not statements and do not cause aswitchcase to be ignored. Always use abreakstatement to ignore aswitchcase.

当一个switch分支仅仅包含注释时,会被报编译时错误。注释不是代码语句而且也不能让switch分支达到被忽略的效果。你应该使用break来忽略某个分支。

The following example switches on aCharactervalue and determines whether it represents a number symbol in one of four languages. For brevity, multiple values are covered in a singleswitchcase.

下面的例子通过switch来判断一个Character值是否代表下面四种语言之一。为了简洁,多个值被包含在了同一个分支情况中。

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."

This example checksnumberSymbolto determine whether it is a Latin, Arabic, Chinese, or Thai symbol for the numbers1to4. If a match is found, one of theswitchstatement’s cases sets an optionalInt?variable calledpossibleIntegerValueto an appropriate integer value.

这个例子检查numberSymbol是否是拉丁,阿拉伯,中文或者泰语中的1到4之一。如果被匹配到,该switch分支语句给Int?类型变量possibleIntegerValue设置一个整数值。

After the switch statement completes its execution, the example uses optional binding to determine whether a value was found. ThepossibleIntegerValuevariable has an implicit initial value ofnilby virtue of being an optional type, and so the optional binding will succeed only ifpossibleIntegerValuewas set to an actual value by one of theswitchstatement’s first four cases.

当switch代码块执行完后,接下来的代码通过使用可选绑定来判断possibleIntegerValue是否曾经被设置过值。因为是可选类型的缘故,possibleIntegerValue有一个隐式的初始值nil,所以仅仅当possibleIntegerValue曾被switch代码块的前四个分支中的某个设置过一个值时,可选的绑定才会被判定为成功。

Because it’s not practical to list every possibleCharactervalue in the example above, adefaultcase handles any characters that are not matched. Thisdefaultcase does not need to perform any action, and so it is written with a singlebreakstatement as its body. As soon as thedefaultcase is matched, thebreakstatement ends theswitchstatement’s execution, and code execution continues from theif letstatement.

在上面的例子中,想要把Character所有的的可能性都枚举出来是不现实的,所以使用default分支来包含所有上面没有匹配到字符的情况。由于这个default分支不需要执行任何动作,所以它只写了一条break语句。一旦落入到default分支中后,break语句就完成了该分支的所有代码操作,代码继续向下,开始执行if let语句。

Fallthrough (贯穿)

Switch statements in Swift don’t fall through the bottom of each case and into the next one. Instead, the entire switch statement completes its execution as soon as the first matching case is completed. By contrast, C requires you to insert an explicitbreakstatement at the end of everyswitchcase to prevent fallthrough. Avoiding default fallthrough means that Swiftswitchstatements are much more concise and predictable than their counterparts in C, and thus they avoid executing multipleswitchcases by mistake.

Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。相比之下,C 语言要求你显式地插入break语句到每个 case 分支的末尾来阻止自动落入到下一个 case 分支中。Swift 的这种避免默认落入到下一个分支中的特性意味着它的switch功能要比 C 语言的更加清晰和可预测,可以避免无意识地执行多个 case 分支从而引发的错误。

If you need C-style fallthrough behavior, you can opt in to this behavior on a case-by-case basis with thefallthroughkeyword. The example below usesfallthroughto create a textual description of a number.

如果你确实需要 C 风格的贯穿的特性,你可以在每个需要该特性的 case 分支中使用fallthrough关键字。下面的例子使用fallthrough来创建一个数字的描述语句。

let integerToDescribe=5

var description="The number\(integerToDescribe)is"

switch integerToDescribe{

case2,3,5,7,11,13,17,19:

d    escription+=" a prime number, and also"

fallthrough

default:

    description+=" an integer."

}

print(description)

// Prints "The number 5 is a prime number, and also an integer."

This example declares a newStringvariable calleddescriptionand assigns it an initial value. The function then considers the value ofintegerToDescribeusing aswitchstatement. If the value ofintegerToDescribeis one of the prime numbers in the list, the function appends text to the end ofdescription, to note that the number is prime. It then uses thefallthroughkeyword to “fall into” thedefaultcase as well. Thedefaultcase adds some extra text to the end of the description, and theswitchstatement is complete.

这个例子定义了一个String类型的变量description并且给它设置了一个初始值。函数使用switch逻辑来判断integerToDescribe变量的值。当integerToDescribe的值属于列表中的质数之一时,该函数在description后添加一段文字,来表明这个数字是一个质数。然后它使用fallthrough关键字来“贯穿”到default分支中。default分支在description的最后添加一段额外的文字,至此switch代码块执行完了。

Unless the value ofintegerToDescribeis in the list of known prime numbers, it is not matched by the firstswitchcase at all. Because there are no other specific cases,integerToDescribeis matched by thedefaultcase.

如果integerToDescribe的值不属于列表中的任何质数,那么它不会匹配到第一个switch分支。而这里没有其他特别的分支情况,所以integerToDescribe匹配到default分支中。

After theswitchstatement has finished executing, the number’s description is printed using theprint(_:separator:terminator:)function. In this example, the number5is correctly identified as a prime number.

当switch代码块执行完后,使用print(_:separator:terminator:)函数打印该数字的描述。在这个例子中,数字5被准确的识别为了一个质数。

Note

Thefallthroughkeyword does not check the case conditions for theswitchcase that it causes execution to fall into. Thefallthroughkeyword simply causes code execution to move directly to the statements inside the next case (ordefaultcase) block, as in C’s standardswitchstatement behavior.

fallthrough关键字不会检查它下一个将会落入执行的 case 中的匹配条件。fallthrough简单地使代码继续连接到下一个 case 中的代码,这和 C 语言标准中的switch语句特性是一样的。

推荐阅读更多精彩内容