Switch
A switch statement contains a value that is compared with several other possible matching patterns, and then executes the corresponding block of code based on the pattern on the successful match. The switch statement provides more options than the IF statement to respond to a variety of potential situations.
One of the simplest examples:
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
}
Each switch statement contains multiple case keywords. In addition, to compare the specified values, Swift provides several different matching patterns for each case, which are described in a later section.
The case body of each switch is a separate code execution branch, which is a bit like the branch of the IF statement. The switch statement determines which branch is selected for execution.
The switch statement must be thoughtful, that is, each possible value is matched to a case. If not, you can define a default case by using the keyword default, which always appears on the last side by default.
The following example uses a switch statement to handle a lowercase character (somecharacter):
Let Somecharacter:character = "E"
Switch Somecharacter {
Case "A", "E", "I", "O", "U":
println ("\ (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":
println ("\ (somecharacter) is a consonant")
Default
println ("\ (Somecharacter) is not a vowel or a consonant")
}
Prints "E is a vowel"
The first case of the switch statement matches all five vowels in English, similar to the second case, which matches the other lowercase letters in English.
It is impractical to write all the other possibilities in a switch case, so the switch statement provides a default case to match other cases, so that the switch statement is as exhaustive as possible.
No implicit Fall through (non-default landing execution)
In contrast to the switch statement in C and OC, the switch statement in Swift does not drop to the next case after each case statement, instead, the entire switch statement ends after the first matching case executes. This makes switch more secure and clearer than the C language, avoiding multiple case execution errors.
Note the point:
You can still jump out of the box before the case executes, if that's what you need.
Each case comparison contains at least one statement that can be executed, and the following code is wrong, because the first case is empty:
Let Anothercharacter:character = "a"
Switch Anothercharacter {
Case "a":
Case "A":
println ("The Letter A")
Default
println ("Not the letter A")
}
This would report a compile-time error
Unlike the C switch statement that does not match "a" and "a", the compiler reports an error, case "a": No execution statement is included. This approach avoids another case falling from one case, making the code more secure and clean.
Multiple matching values in a switch case can be separated by commas (,), or divided into multiple lines:
Switch some value to consider {
Case value 1,
Value 2:
Statements
}
Note the point:
In order for a particular switch to have drop functionality, you can use the keyword Fallthrough.
Range Matching ( range matching)
In a switch case, you can check whether the value is contained in a range. The following example uses a numeric range to provide natural language information about any size number:
Let count = 3_000_000_000_000
Let countedthings = "stars in the Milky"
var naturalcount:string
Switch Count {
Case 0:
Naturalcount = "No"
Case 1...3:
Naturalcount = "a few"
Case 4...9:
Naturalcount = "several"
Case 10...99:
Naturalcount = "tens of"
Case 100...999:
Naturalcount = "hundreds of"
Case 1000...999_999:
Naturalcount = "thousands of"
Default
Naturalcount = "millions and millions of"
}
println ("There is \ (Naturalcount) \ (countedthings).")
Prints "There is millions and millions of stars in the Milky."
tuples (meta- group )
You can use tuples in the same switch statement to test multiple values. Each element in a tuple can be used to test different values or ranges. You can also use an underscore (_) to identify any possible matching values.
In the following example, use a simple (int,int) type tuple with a point (x, y) to represent it, and its position on the graph:
Let Somepoint = (1, 1)
Switch Somepoint {
Case (0, 0):
println ("(0, 0) is at the origin")
Case (_, 0):
println ("(\ (somepoint.0), 0) is on the x-axis")
Case (0, _):
println ("(0, \ (somepoint.1)) is on the y-axis")
Case ( -2...2, -2...2):
println ("(\ (somepoint.0), \ (Somepoint.1)) is inside the box")
Default
println ("(\ (somepoint.0), \ (Somepoint.1)) is outside of the box")
}
Prints "(1, 1) is inside the box"
"Let Somepoint = (1, 1)
Switch Somepoint {
Case (0, 0):
println ("(0, 0) is at the origin")
Case (_, 0):
println ("(\ (somepoint.0), 0) is on the x-axis")
Case (0, _):
println ("(0, \ (somepoint.1)) is on the y-axis")
Case ( -2...2, -2...2):
println ("(\ (somepoint.0), \ (Somepoint.1)) is inside the box")
Default
println ("(\ (somepoint.0), \ (Somepoint.1)) is outside of the box")
}
Prints "(1, 1) is inside the box"
In the example, the switch statement determines whether the point is at the origin (0,0), whether it is in the red X-axis, whether in the Orange y-axis, in the Blue 4*4 box, or outside the box.
Unlike switch in C, which allows multiple cases to handle the same value. In fact, the above example Zhongyuan Point (0,0) can match all four situations, however if multiple matches, then the first matching case is always selected, so the point (0,0) will match to case (0, 0), Therefore, all other matches are ignored.