Explain the use of switch...case statements in Swift programming _swift

Source: Internet
Author: User

Switch as an essential statement in the selection structure is also added to Swift, as long as people with programming experience are not unfamiliar with switch statements, but Apple has greatly enhanced the switch to have features that are not available in other languages. Using the switch statement is simple:

var i = 0 
switch I {case 
 0: 
  "0"//Output Case 
 1: 
  "1" 
 Default: 
  "2" 
} 

In this simplest switch statement, the one thing that is very different from other languages is that you don't have to explicitly add a break. Swift's switch statement automatically adds a break at the end of the case, and automatically quits when the case part of the condition is fulfilled. But in some cases, we might want the switch to execute two case at a time, so you can:

var i = 3 
switch I {case 
 0, 3: 
  "0"//Output Case 
 1: " 
  1" 
 default: 
  "2" 
} 

In multiple conditions after the case, separate them with commas.
If you want to continue with the next case after you've done a single one, you'll need to use a new keyword:

var i = 0 
switch I {case 
 0: 
  "0"//output 
  Fallthrough case 
 1: 
  "1"//Output Case 
 2: 
  "2" 
 default: 
  ' Default ' 
} 

Using the New keyword Fallthrough enables the switch to execute the next case immediately after the execution of one of the cases.
Swift's switch statement must cover all situations, which is not to say that there must be default, so long as the above case satisfies all the circumstances, you do not have to write default.


Let's take a look at some small examples to help understand:

Example 1:

Let SomeOne = ("M", "Liyuanbo")

switch someOne
 {case
(_,let name) where Name.hasprefix ("Li"):
 println ("name has Li")
 
Default:
 println ("has not Li")
}

Here (_,let name) where Name.hasprefix ("Li") is the name in someOne2 Fall (_,let name) has no prefix string or no li. Of course someOne2 is satisfied with this condition.


Example 2:
let's look at the use of Fallthrough.

Switch coordinate1
 {case
(0,0):
 println ("origin")
 Fallthrough case
(_,0):
 println ("x axis"
 Fallthrough Case
(0,_):
 println ("Y-axis") Case
( -2...2,-2...2):
 println ("rectangular area")
Default:
 println ("No target area")
 
}

So we can see the printout: The origin, the x axis, the y-axis, and if you continue to add, you will print out a rectangular area.

Finally, we would like to emphasize that the switch in Swift contains all the circumstances of the variable, where if Coordinate4 is (10,10) and then the default comment point, the error is made because the above 4 conditions are not satisfied (10,10) this point.


Does that mean that you have to have default, of course not, that you want to include all of the variables, and you can have no default.

Let Coordinate2 = (2,0)

switch coordinate2
 {case
(0,0):
 println ("origin") case
(Let x,0):
 println ("x-axis") Case
(0,let y):
 println ("Y-axis") Case
 
(Let X,let y):
 println ("All Areas")
 
}

Here you can have no default, because the case here (let X,let y) contains all the points of the 2-D coordinate.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.