switch on other types
Swift โฉ Pattern Matching โฉ Sentence Patterns โฉ
// CGPoint
let point = CGPoint(x: 7, y: 0)
// switch on Tuple
switch (point.x, point.y) {
case (0, 0): print("On the origin!")
case (0, _): print("x=0: on Y-axis!")
case (_, 0): print("y=0: on X-axis!")
case let (x, y) where x == y:
print("On y = x")
default:
print("Quite a random point here.")
}// Range<Int>
switch count {
case Int.min..<0: print("Negative count, really?")
case 0 : print("Nothing")
case 1 : print("One")
case 2..<5 : print("A few") // use `2..<5 ~= count` behind the scene
case 5..<10: print("Some")
default : print("Many")
}
//
switch char {
case
"A", "E", "I", "O", "U", "Y",
"a", "e", "i", "o", "u", "y": // ...
case
"A"..."Z",
"a"..."z": // ...
default: // ...
}๐พ ็จๅผ๏ผ replit
AliSoftware โฉ Pattern Matching
operator (~=) - pattern matching operator.
Last updated
Was this helpful?