Skip to content

Instantly share code, notes, and snippets.

@mdznr
Last active August 29, 2015 14:02
Show Gist options
  • Save mdznr/dbfa4bdfcb45f0f9dc3b to your computer and use it in GitHub Desktop.
Save mdznr/dbfa4bdfcb45f0f9dc3b to your computer and use it in GitHub Desktop.
The way switch statements work is different in Swift than in other languages, like C.
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.
@mdznr
Copy link
Author

mdznr commented Jun 3, 2014

This is going to cause issues for handling cases of UIGestureRecognizerState. A lot of times code for UIGestureRecognizerStateChanged is added to UIGestureRecognizerStateBegan. This will result in duplicated code.

@manolosavi
Copy link

you could use 'continue' to flow to the next statement in Swift though, no?

@soffes
Copy link

soffes commented Jun 3, 2014

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.

@pwightman
Copy link

Why do you need multiple cases if you can comma-separate multiple things in a single case?

@mdznr
Copy link
Author

mdznr commented Jun 3, 2014

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;
}

@mdznr
Copy link
Author

mdznr commented Jun 4, 2014

Ahh, I found out there's a fallthrough keyword. I thought that was missing. All is resolved!

@ArtSabintsev
Copy link

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!

@Blackjacx
Copy link

Wow thats great! Hated writing break under each case statement!
Nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment