SoFunction
Updated on 2025-04-11

Switch statement in swift in swift

I won’t say much nonsense, I will just post the code to you. The specific code is as follows:

 /**
      switch statement
      */
    let str = "aAbBacdef"
    let str2 = "aAbBadef"
    let str3 = "aAbBadeff"
//    var array = [];
    for c in ["A", "a", str3]
    {
      switch c {
//      case "a":
      case "a", "A":
        print("ldd")
        
      // Must have      default:
        print("dd")
      }
    }
    /**
      case "a":
      case "A":
      print("ldd")
      In C language, print("ldd") will be executed no matter if a A is encountered;
      This is not allowed in Swift, but it can be written like this
      case "a", "A": Separate the middle with commas
      */
//    switch value {
//    case pattern:
//      code
//    default:
//      code
//    }
    /**
      In c language
      There is a break below the case;
      If you forget to write break, the following statements will be executed sequentially until break is executed;
      But the swift language just sees this, don't break it.  After comparing the conditions in the case,
      The switch statement will automatically exit after execution.
      If you want to continue executing, use fallthrough
      */

The above is the switch statement in Swift in Swift introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!