# Tutorial by Examples

## Basic Use

let number = 3 switch number { case 1: print(&quot;One!&quot;) case 2: print(&quot;Two!&quot;) case 3: print(&quot;Three!&quot;) default: print(&quot;Not One, Two or Three&quot;) } switch statements also work with data types other than integers. They work with any data t...

## Matching Multiple Values

A single case in a switch statement can match on multiple values. let number = 3 switch number { case 1, 2: print(&quot;One or Two!&quot;) case 3: print(&quot;Three!&quot;) case 4, 5, 6: print(&quot;Four, Five or Six!&quot;) default: print(&quot;Not One, Two, Three, Four, F...

## Matching a Range

A single case in a switch statement can match a range of values. let number = 20 switch number { case 0: print(&quot;Zero&quot;) case 1..&lt;10: print(&quot;Between One and Ten&quot;) case 10..&lt;20: print(&quot;Between Ten and Twenty&quot;) case 20..&lt;30: print(&quot;Be...

## Using the where statement in a switch

The where statement may be used within a switch case match to add additional criteria required for a positive match. The following example checks not only for the range, but also if the number is odd or even: switch (temperature) { case 0...49 where temperature % 2 == 0: print(&quot...

## Satisfy one of multiple constraints using switch

You can create a tuple and use a switch like so: var str: String? = &quot;hi&quot; var x: Int? = 5 switch (str, x) { case (.Some,.Some): print(&quot;Both have values&quot;) case (.Some, nil): print(&quot;String has a value&quot;) case (nil, .Some): print(&quot;Int has a value&...

## Partial matching

Switch statement make use of partial matching. let coordinates: (x: Int, y: Int, z: Int) = (3, 2, 5) switch (coordinates) { case (0, 0, 0): // 1 print(&quot;Origin&quot;) case (_, 0, 0): // 2 print(&quot;On the x-axis.&quot;) case (0, _, 0): // 3 print(&quot;On the y-axis.&quot;) ca...

## Switch fallthroughs

It is worth noting that in swift, unlike other languages people are familiar with, there is an implicit break at the end of each case statement. In order to follow through to the next case (i.e. have multiple cases execute) you need to use fallthrough statement. switch(value) { case 'one': //...

## Switch and Enums

The Switch statement works very well with Enum values enum CarModel { case Standard, Fast, VeryFast } let car = CarModel.Standard switch car { case .Standard: print(&quot;Standard&quot;) case .Fast: print(&quot;Fast&quot;) case .VeryFast: print(&quot;VeryFast&quot;) } Since we ...

## Switch and Optionals

Some example cases when the result is an optional. var result: AnyObject? = someMethod() switch result { case nil: print(&quot;result is nothing&quot;) case is String: print(&quot;result is a String&quot;) case _ as Double: print(&quot;result is not nil, any value that is a Dou...

## Switches and tuples

Switches can switch on tuples: public typealias mdyTuple = (month: Int, day: Int, year: Int) let fredsBirthday = (month: 4, day: 3, year: 1973) switch theMDY { //You can match on a literal tuple: case (fredsBirthday): message = &quot;\(date) \(prefix) the day Fred was born&quot;...

## Matching based on class - great for prepareForSegue

You can also make a switch statement switch based on the class of the thing you're switching on. An example where this is useful is in prepareForSegue. I used to switch based on the segue identifier, but that's fragile. if you change your storyboard later and rename the segue identifier, it breaks ...

Page 1 of 1