How it works...

Within the lookTowards function, we want to print a different message for each possible CompassPoint case; to do this, we can use a switch statement:

func lookTowards(_ direction: CompassPoint) {          switch direction {     case .north:         print("To the north lies a winding road")     case .south:         print("To the south is the Prancing Pony tavern")     case .east:         print("To the east is a blacksmith")     case .west:         print("The the west is the town square")     }}

At the top of the switch statement, we define the value that we want to switch on; then we define what we want to be done when that value matches each of the defined cases using the case keyword and then the matching pattern:

switch <#value#> { case <#pattern#>:     <#code#> case <#pattern#>: ...

Get Swift 4 Programming Cookbook 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.