-
-
Save mdznr/dbfa4bdfcb45f0f9dc3b to your computer and use it in GitHub Desktop.
var total : Int | |
let myString = "foo" | |
// When originally looking at Swift's switch statements, you may think this is how you get fall through behaviour: | |
total = 0 | |
switch myString { | |
case "foo": | |
total += 4 | |
case "foo", "bar": | |
total += 2 | |
default: | |
total += 1 | |
} | |
total // 4 | |
// But that didn't give you the expected value of 6 for `total`. So maybe you go and try this: | |
total = 0; | |
switch myString { | |
case "foo", "bar": | |
total += 2 | |
case "foo": | |
total += 4 | |
default: | |
total += 1 | |
} | |
total // 2 | |
// That still didn't work. Only the first match is executed. Here's where the `fallthrough` keyword comes in: | |
total = 0; | |
switch myString { | |
case "foo": | |
total += 4 | |
fallthrough | |
case "bar": | |
total += 2 | |
default: | |
total += 1 | |
} | |
total // 6 | |
// `fallthrough` is basically the opposite of `break` in C. Falling through is now opt-in instead of opt-out. This is one example of how Swift is more modern than C. In modern uses of switch statements, almost all of them want the break behaviour. Make this default and reduce code error. |
you could use 'continue' to flow to the next statement in Swift though, no?
I think this is great. You almost never want it to fall through. If continue
does make it fall through, I think that's great. Opting in instead of opting out is way better in my opinion.
Why do you need multiple cases if you can comma-separate multiple things in a single case?
Yeah, I definitely agree that break
by default is fantastic, because that's what most uses of it do. It just removed the other functionality entirely, from what I can tell.
continue
is only allowed inside of a loop.
You still need multiple cases if you want part of a block of code shared between two case statements. The example I used in the first comment mentions that sometimes you need special code for UIGestureRecognizerStateBegan
but also want it to execute the code for UIGestureRecognizerStateChanged
. Here's how that was handled in Objective-C:
switch( gestureRecognizer.state ) {
case UIGestureRecognizerStateBegan:
// Add or change visual appearance of a view
case UIGestureRecognizerStateChanged:
// Translate view.
default:
break;
}
Ahh, I found out there's a fallthrough
keyword. I thought that was missing. All is resolved!
This is awesome, @mdznr! So they fundamentally changed how Switch
works. In C languages, fallthrough
was the default effect if we didn't add a break
. In Swift, you have to specify fallthrough
, to get the effect of sharing conditional changes in case statements. That's smarter in my opinion. Less of a chance to make a mistake!
Wow thats great! Hated writing break under each case statement!
Nice :)
This is going to cause issues for handling cases of
UIGestureRecognizerState
. A lot of times code forUIGestureRecognizerStateChanged
is added toUIGestureRecognizerStateBegan
. This will result in duplicated code.