Examples of Swift programming in switch... The use of case statements

  • 2020-05-13 03:32:58
  • OfStack

Switch has also been added to Swift as an essential part of the selection structure. Anyone with programming experience will be familiar with Switch, but apple has greatly enhanced Switch to include features not found in other languages. Using the Switch statement is simple:


var i = 0 
switch i { 
 case 0: 
  "0" // Is the output  
 case 1: 
  "1" 
 default: 
  "2" 
} 

In this simplest Switch statement, one thing that is very different from other languages is that you do not explicitly add break. The Switch statement of Swift automatically adds break at the end of case, and exits automatically after executing the part of case that meets the criteria. But in some cases, we might want Switch to execute two case at the same time, so we could do this:


var i = 3 
switch i { 
 case 0,3: 
  "0" // Is the output  
 case 1: 
  "1" 
 default: 
  "2" 
} 

Separate multiple conditions after case with a comma.
If you want to continue to execute the next case after executing one case, you need to use a new keyword:


var i = 0 
switch i { 
 case 0: 
  "0" // Is the output  
  fallthrough 
 case 1: 
  "1" // Is the output  
 case 2: 
  "2" 
 default: 
  "default" 
} 

Using the new keyword fallthrough enables Switch to execute one case followed by the next case.
Swift's Switch statement 1 must cover all cases. This does not mean that 1 must have default. As long as case above satisfies all cases, default is not required.


Here are a few examples to help you understand:

Case 1:


let someOne = ("25","liyuanbo")

switch someOne
 {
case (_,let name) where name.hasPrefix("li"):
 println("First name has li")
 
default:
 println("First has not li")
}

Here (_,let name) where name.hasPrefix ("li") is name in someOne2 falling into (_,let name) with no prefix string and no li. Of course, someOne2 satisfies this condition.


Example 2:
Let's take a look at fallthrough in action


switch coordinate1
 {
case (0,0):
 println(" The origin ")
 fallthrough
case (_,0):
 println("x shaft ")
 fallthrough
case(0,_):
 println("y shaft ")
case(-2...2,-2...2):
 println(" Rectangular area ")
default:
 println(" There is no target area ")
 
}

So we can see the print output: the origin, x axis,y axis, if you continue to add, you will print an extra rectangle.

Finally, we want to emphasize the point 1, that switch1 in swift definitely contains all the cases of variables, here if coordinate4 is (10,10) and then we annotate default, we will report an error, because the above four conditions cannot satisfy the point (10,10).


Does that mean you have to have default, of course not, it has to be for all cases of the variable, you can do without default.


let coordinate2 = (2,0)

switch coordinate2
 {
case (0,0):
 println(" The origin ")
case (let x,0):
 println("x shaft ")
case(0,let y):
 println("y shaft ")
 
case(let x,let y):
 println(" All areas ")
 
}

You don't have to have default here, because case(let x,let y) has all the points in 2 dimensional coordinates.


Related articles: