标签:
之前记录了Swift Enumeration(1),这篇算是它的延续吧,继续说下Enumeration,看以下定义
enum TrainStatus {
case OnTime
case Delay(Int) //带关联值
}
var myTrainStatus = TrainStatus.Delay(10)
用Switch 匹配对象
switch myTrainStatus{
case .OnTime:
println("Train is ontime")
case .Delay(let minutes):
println("Train delays \(minutes) minutes")
}
匹配方式是有先后顺序的,上面中先匹配到Delay然后再给里面的minutes赋值,好下面我将演示区间匹配。
switch myTrainStatus{
case .OnTime://匹配准时
println("Train is ontime")
case .Delay(1...5)://匹配1到5
println("Train delays less than five minutes")
case .Delay(6...10)://匹配6到10
println("Train delays between six and ten minutes")
case .Delay(_): //匹配其它,里面是下划线
println("Train delays more than ten minutes")
}
标签:
原文地址:http://www.cnblogs.com/foxting/p/4417837.html