SoFunction
Updated on 2025-04-06

Switch statement interval and tuple pattern matching in Swift

I won’t say much nonsense. The following code introduces the switch statement interval and tuple pattern matching. The specific content is as follows:

// Generalized matching of switch    
    let x = 1000
    
    // In other words, it is not required that the switch is followed by integer constants as C language.    
    switch x {
      
      // The case can be followed by the interval    case 1...9:
      print("Single digits")
    case 10...99:
      print("Ten digits")
    case 100...999:
      print("Hundred digits")
    case 1000...9999:
      print("Thousand digits")
      
    default:
      print("Not in line")
    }
    
    
    
    let point = (10, 10)
    // A tuple type can be followed by the switch    switch point {
    case (0, 0):
      print("Coordinate Origin")
    case (1...10, 1...10):
      print("The x and y coordinate ranges are between 1-10")

    case(_, 0):
      print("Point on the x-axis")
      
    default:
      print("other")
    }

The above is the matching of switch statement intervals and tuple pattern 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!