SWIFT Enumeration(2)

來源:互聯網
上載者:User

標籤:

之前記錄了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")

}

SWIFT Enumeration(2)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.