23 Feb 2016   swift


In Appleā€™s Swift language switch statements must be what apple calls ā€œexhaustiveā€. Iā€™ve felt the term to be very literal. Literally exhaustive?

Example that does not work:

let count = 42

switch count {
case 1:
  print(1)
case 7:
  print(7)
}

The above statement does not work because itā€™s missing a default case. Why? What if I donā€™t want to do anything else? Why do I need to write something that wonā€™t be used? Donā€™t worry, there is an amazing and less ā€œexhaustiveā€ way to handle these situations; simply default: ()

Correct example:

let count = 42

switch count {
case 1:
  print(1)
case 7:
  print(7)
default: ()
}
šŸ„