Explain the use of the break keyword in Swift's switch...case statement _swift

Source: Internet
Author: User
Tags case statement

The switch in Swift has been greatly improved in comparison with this part of the objective-c. This is a very interesting thing, because it is still not added to the objective-c, or the fact that the objective-c is a superset of C is not broken.

The first exciting place is that you can convert to a string. This may be what you want to do, but you can't do. In Objective-c, if you want to use a "switch" to a string, you must use multiple if statements and use Isequaltostring: as follows:

if ([Person.name isequaltostring:@ "Matt Galloway"]) {
 NSLog (@ "Author of a interesting Swift article");
} else if ([Person.name isequaltostring:@ "Ray Wenderlich"]) {
 NSLog (@ "has a great website");
} else if ([Person.name isequaltostring:@ "Tim Cook"]) {
 NSLog (@ "CEO of Apple Inc.");
} else {
 NSLog (@ "Someone E LSE);
}

This is not strong reading, but also to play a lot of words. The same functionality is implemented in Swift as follows:

Switch Person.name {case
 "Matt Galloway":
 println ("Author of a interesting Swift article") Case
 "Ray Wen Derlich ":
 println (" has a great website ") Case
 " Tim Cook ":
 println (" CEO of Apple Inc. ")
 Default:
 println ("Someone Else")
}

In addition to being able to use a switch for strings, note here are some interesting things to do. Did not see the break. Because the execution of a case statement in the switch is not done down. No more chance to appear bug!

And for instance, in this case,

Let vegetable = ' red pepper '
switch vegetable{case
 ' celery ': let
  vegetablecomment = "Add some raisins and mak e ants on a log. "
 Case "cucumber" and "watercress": let
  vegetablecomment = "This would make a good tea sandwich."

 
 The switch supports all types of data, as well as a variety of comparison operations--not limited to integers, and no limits to the need to test for equality (Tests for equality really translates this way?). Case let
 x where X.hassuffix ("Pepper"): let
  vagetablecomment = "is it a spicy \ (x)?"

 Switch statement requirements must overwrite all possible, otherwise the error ' switch must be exhaustive, consider adding a default CLA '
 default:
  print (" Cannot have default ")
}

You don't need to write a break,
After you finish matching the case, the program jumps out of the switch instead of continuing with the next case, so you don't need to add a break behind the case's code to jump out of the switch.

The following switch statement may disrupt your thinking, so be prepared!

Switch I {case
0, 1, 2:
 println ("Small") Case
3...7:
 println ("Medium") Case
8..10:
 println (" Large ") Case
_ where I% 2 = 0:
 println (" even ") case
_ where I% 2 = 1:
 println (" Odd ")
Default:
   break
}

First, there is a break. Because the switch must be comprehensive and thorough, they need to deal with all the situations. In this case, we want to default without doing anything, so we put a break to show that nothing should happen here.
The next interesting thing is what you see above ... And.., these are the new operators used to define the scope. The former is used to define the range including the right side of the number, which defines the range does not include the right number. They are incredibly useful.

The last thing you can do is define the case as a calculation of the input value. In this case, if this value does not match from 0 to 10, if it is even printed "even", it is odd to print "Odd". It's amazing!

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.