Swift's switch... The use of the break keyword in an case statement

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

Compared with this part of Objective-C, switch is greatly improved in Swift. This is a very interesting thing, because it's still not added to Objective-C, and it still doesn't break the fact that Objective-C is a superset of C.

The first exciting thing is that you can convert strings. This may be exactly what you wanted to do, but couldn't. To use "switch" for strings in Objective-C, you must use multiple if statements and isEqualToString:, as follows:

if ([person.name isEqualToString:@"Matt Galloway"]) {
 NSLog(@"Author of an 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 else);

It's not very readable, and it takes a lot of typing. The same functionality is implemented in Swift as follows:

switch person.name {
 case "Matt Galloway":
 println("Author of an interesting Swift article")
 case "Ray Wenderlich":
 println("Has a great website")
 case "Tim Cook":
 println("CEO of Apple Inc.")
 println("Someone else")

In addition to using switch for strings, notice something interesting here. break not seen. Because in switch, an case statement does not execute down after execution. No more bug by accident!

Let's say I have a situation like this

let vegetable = "red pepper"
switch vegetable{
 case "celery":
  let vegetableComment = "Add some raisins and make ants on a log."
 case "cucumber","watercress":
  let vegetableComment = "That would make a good tea sandwich."

 //switch Supports all types of data, as well as a variety of comparison operations -- there is no limit to having to be integers, and there is no limit to having to test for equality (tests for equality  Is that really the translation? )
 case let x where x.hasSuffix("pepper"):
  let vagetableComment = "Is it a spicy \(x)?"

 //switch The statement requires that all possibilities be covered, otherwise an error is reported 'switch must be exhaustive, consider adding a default cla'
  print(" We can not live without default")

You don't have to say break,
After executing the matched case, the program will jump out of switch instead of continuing with the next case, so there is no need to add break to case's code to jump out of switch.

The following switch statement may mess with your thoughts, so get ready!

switch i {
case 0, 1, 2:
case 3...7:
case 8..10:
case _ where i % 2 == 0:
case _ where i % 2 == 1:

First, there's an break. Because the switch must be comprehensive and thorough, they need to handle all situations. In this case, we don't do anything when we want default, so we put an break to indicate that nothing should happen here.
The next interesting thing is what you see up there... And.. , these are the new operators used to define the scope. The former is used to define the range that includes the number on the right, and the latter does not include the number on the right. They are incredibly useful.

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

Related articles: