http://austinzheng.com/2014/12/16/swift-pattern-matching-switch/
A switch statement compares some value so it can compare it against several matching patterns.
evaluating a single item reacting to multiple possible values. Switch statements must be exhaustive. Can provide range of values with range operator
switch valueToConsider {
case value1:respond to value1
case value2: respond to value2
default: do something else
}
using an array and for in:
let airportCodes = ["LGA", "LHR", "CDG", "HKG", "DXB", "JKL"]
for airportCode in airportCodes {
switch airportCode {
case "LGA", "SFO": print("La Guaria")
case "LHR": print("That is lhr")
default: print("I Dont know")
}
}
You can test on multiple cases separated by a comma
You can also switch over a range of values:
var randomTemp = Int(arc4random_uniform(UInt32(150)))
switch randomTemp {
case 0..<32: print("Dont leave home")
case 32...45: print("You will need a jacket")
case 45...70: print("You will need a swearer")
default: print("Too hot")
}
You can also run a switch on key, value pairs:
let world = [
"BEL": "Brussels",
"LIE": "Vaduz",
"BGR": "Sofia",
"USA": "Washington D.C.",
"MEX": "Mexico City",
"BRA": "Brasilia",
"IND": "New Delhi",
"VNM": "Hanoi"]
for (key, value) in world {
// Enter your code below
switch key {
case "BEL", "LIE", "BGR": europeanCapitals.append(value)
case "IND", "VNM": asianCapitals.append(value)
default: otherCapitals.append(value)
}
// End code
}