The switch statement

A switch statement is a concise way to describe a situation where we have several possible options to pick from and we don't want to write a lot of boilerplate code using the already familiar if statement.

Here is the general pattern of a switch statement (please note that this is not a valid Swift code):

switch a-variable-to-be-matched {
case value-1:
//code which will be executed, if variable has value-1
//we need at least one valid executable statement here
(comments are not an executable statement)
case value-2,
value-3:
//code which will be executed, if variable has value-2 or value-3
default:
//code which will be executed, if variable has value different
from all listed cases
}

What we see is that switch has many possible cases, each one starting with the special word case, and then a specific value. Swift supports specific value matching, but it supports more complex rules for pattern matching. Each case could be considered as a separate if statement. If one case is activated, then all others are skipped. The default case is a specific one and is triggered if there is no match with any other case. The default case appears at the end, and it's defined with the special word default.

We can use break to interrupt execution of the code in a case statement. If we want to have an empty case statement, it's good to add break.

We have some specifics with the implementation of switch in Swift, which are new when compared to the other programming languages, but they improve the readability of the code. First, there is now a way to have an empty body of a specific case. To be correct, we have to add at least one valid statement after the case. There is no implicit fallthrough after each case. This means that once the last executable statement in a case branch is triggered, we are continuing after the switch statement. Nothing else that is part of the switch statement will be executed. We could consider that every case statement has a hidden break at its very end. Next, we need the special word fallthrough to simulate the regular behavior of the switch. Another interesting thing is that we can have interval matching, tuples matching, and value bindings. Finally, we can use the where clause if we want to express some dependency between the data which should be matched. It's also possible to list several cases if they have to share the code which should be executed. They have to be separated with ,.

Here is code that shows how easy and smart switch is:

let point = (1, 1)
switch point {
case let (x, y) where x == y:
print("X is (x). Y is (y). They have the same value.");
case (1, let y):
print("X is 1. Y is (y). They could be different.");
case (let x, 1):
print("X is (x). Y is 1. They could be different.");
case let (x, y) where x > y:
print("X is (x). Y is (y). X is greater than Y.");
default:
print("Are you sure?")
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.164.141