In Swift, once a switch condition is reached it does implicitly "break" and get out of the switch case. In other terms it does not continues to the next condition one. How to achieve the regular behaviour as in C, C++, java, javascript etc… ?
Asked
Active
Viewed 1.1k times
1 Answers
23
Taken from the Apple Swift documentation:
If you really need C-style fallthrough behavior, you can opt in to this behavior on a case-by-case basis with the fallthrough keyword. The example below uses fallthrough to create a textual description of a number:
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough // explicitly tells to continue to the default case
default:
description += " an integer."
}
println(description)
// prints "The number 5 is a prime number, and also an integer."

Flavien Volken
- 19,196
- 12
- 100
- 133
-
So weird that this exists. Thanks but I don't think this should be used. – Alper Feb 07 '17 at 14:24