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  w w. j a  va2s . co  m*/
    print("Huh?") 
}

Result

Related Topic