Previously recorded by Swift enumeration (1), this is the continuation of it, continue to say enumeration, see the following definition
Enum Trainstatus {
Case OnTime
Case Delay (INT)//with associated values
}
var mytrainstatus = Trainstatus.delay (10)
Match objects with switch
Switch mytrainstatus{
Case. OnTime:
println ("Train is OnTime")
Case. Delay (Let minutes):
println ("Train delays \ (minutes) minutes")
}
The matching method is sequential, the above first match to delay and then to the inside of the minutes assignment, OK below I will demonstrate interval matching.
Switch mytrainstatus{
Case. ontime://Match on time
println ("Train is OnTime")
Case. Delay (1...5)://Match 1 to 5
println ("Train delays less than five minutes")
Case. Delay (6...10)://Match 6 to 10
println ("Train delays between six and ten minutes")
Case. Delay (_)://Match other, inside is underline
println ("Train delays more than ten minutes")
}
SWIFT Enumeration (2)