Swift - Switches on String value

Introduction

Switches in Swift aren't just for numbers.

You can switch across all types in swift.

For example, a switch statement for a string would look something like the following:

Demo

let greeting = "Hello" 
switch greeting { 
case "Hello": 
    print("Oh hello there.") 
case "Goodbye": 
    print("Sorry to see you leave.") 
default: // w  ww .  ja  v  a 2  s .  co m
    print("Huh?") 
}

Result

Related Topic