Barnash Barnash - 2 months ago 11
Swift Question

How can I write an empty case in Swift?

Since swift does not fallthrough

case
statements in
switch
, How can I write an empty case statement without getting an error?

let a = 50
switch a {
case 0..10:
case 10..100:
println("between 10 and 100")
default:
println("100 and above")
}


How can I make the first case stop the switch?

Answer
let a = 50
switch a {
case 0..10:
    break // Break the switch immediately
case 10..100:
    println("between 10 and 100")
default:
    println("100 and above")
}

Keyword break is optional, but not in this case :)

Comments