Swift programming switch... case statement instance resolution

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

The switch Swift... The case statement determines the object type, whereas the Objective-C statement must be an integer.
It can't go through, it can't say break,


var rank = "A"
switch rank{
  case "A": // The equivalent of if
    print(" optimal ")
  case "B": //  The equivalent of else if
    print(" optimal ")
  case "C": //  The equivalent of else if
    print(" optimal ")
  default: //  The equivalent of else
    print(" There is no rating ")
}


You can't do that because you can't penetrate


var rank1 = "A"
switch rank1{
  case "A":
  case "B":
    print(" optimal ")
  case "C":
    print(" optimal ")
  default:
    print(" There is no rating ")
}


That's the only way to write it


var rank1 = "A"
switch rank1{
  case "A", "B": //  Pay attention to OC You can't write it like that 
    print(" optimal ")
  case "C":
    print(" poor ")
  default:
    print(" There is no rating ")
}

I can't stop writing default


var rank2 = "A"
switch rank2{
  case "A":
    print(" optimal ")
  case "B":
    print(" good ")
  case "C":
    print(" poor ")
}

The default position can only be at the end


var rank3 = "A"
switch rank3{
  default:
    print(" There is no rating ")
  case "A":
    print(" optimal ")
  case "B":
    print(" good ")
  case "C":
    print(" poor ")
}

Variables are defined in case without the use of parentheses


var rank4 = "A"
switch rank4{
  case "A":
    var num = 10
    print(" optimal ")
  case "B":
    print(" good ")
  case "C":
    print(" poor ")
  default:
    print(" There is no rating ")
}

The interval matches the primitive


var num = 10;
switch num{
  case 1...9:
    print(" Single digits ")
  case 10...99:
    print("10 digits ")
  default:
    print(" Several other ")
}

var point = (10, 15)
switch point{
  case (0, 0):
    print(" At the origin ")
  case (1...10, 10...20): //  You can add intervals to the progenitors 
    print(" Coordinates of the X and Y in 1~10 between ")
  case (_, 0): // X It could be any number 
    print(" Coordinates of the X in X On the shaft ")
  default:
    print("Other")
}

Value binding


var point = (1, 10)
switch point{
  case (var x, 10): //  will point In the X Is assigned to X
    print("x= \(x)")
  case (var x, var y): //  will point In the XY Is assigned to XY
    print("x= \(x) y= \(y)")
  case var( x, y):
    print("x= \(x) y= \(y)")
  default:
    print("Other")
}

Bind by condition


var point = (100, 10)
switch point{
  //  only where The following conditional expression evaluates and executes true talent case After the statement of 
  case var(x, y) where x > y: 
    print("x= \(x) y= \(y)")
  default:
    print("Other")
}

fallthrough keyword
switch in Swift does not fall from the previous case branch to the next case branch. Instead, as long as the first matched case branch completes the statement it needs to execute, the entire switch block completes its execution. In contrast, C requires you to insert break statements to the end of each switch branch to prevent them from automatically falling into the next case branch. This ability of Swift to avoid default to the next branch means that its switch functionality is clearer and more predictable than that of C, avoiding errors caused by inadvertently executing multiple case branches.

If you really need the C style drop (fallthrough) feature, you can use the fallthrough keyword in every case branch that needs the feature. The following example USES fallthrough to create a one-number description statement.


var rank1 = "A"
switch rank1{
  case "A":
  case "B":
    print(" optimal ")
  case "C":
    print(" optimal ")
  default:
    print(" There is no rating ")
}
0

This example defines a variable of type String, description, and sets it an initial value. The function USES switch logic to determine the value of the integerToDescribe variable. When the value of integerToDescribe belongs to one of the prime Numbers in the list, the function adds a paragraph of text after description to indicate that the number is a prime number. It then USES the fallthrough keyword to "drop" into the default branch. The default branch adds an extra bit of text at the end of the description, so the switch block is done.

If the value of integerToDescribe does not belong to any prime number in the list, it will not match to the first switch branch. There are no other special branching cases, so integerToDescribe matches all of the default branches.

When the switch code block is finished executing, use the println function to print a description of the number. In this example, the number 5 is accurately identified as a prime number.

NOTE: the fallthrough keyword does not check it and the next one will fall into the match condition in the case execution. fallthrough simply makes the code execution continue to connect to the executing code in the next case, just like the switch statement feature in the C language standard.


Related articles: