Handling all cases with switch
switch
statements allow you to control the flow of execution by testing one specific value in multiple ways. In Objective-C and other languages, switch
statements can only be used on values that can be represented by an integer, and they are most commonly used to make decisions based on enumeration cases.
As we have seen, enumerations have become a lot more powerful in Swift, as they can be based on more than just integers, and so too can switch
statements.
switch
statements in Swift can be used on any type and have advanced pattern-matching functionality.
In this recipe, we will explore both simple and advanced usage of switch
control flow statements to control logic.
Getting ready
If you are old enough to remember the early days of the home computer, you may also remember text-based adventures. These were simple games that usually described a scene and then let you move around, by typing a command to move north, south, east, or west...