Switching It Up: switch Statements

You get much more control if you use a switch statement to control your flow instead of using tons of if else statements. Swift’s switch statements are very similar to those in other languages with some extra power added in. One little caveat is that in Swift, you do not have to use break to stop a condition from running through each case statement. Swift automatically breaks on its own when the condition is met.

Another caveat about switch statements is that they must be exhaustive. That is, if you are using a switch statement on an int, then you need to provide a case for every int ever. Because this is not possible, you can use the default statement to provide a match when nothing else matches. Here is a ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.