enum TrainStatus {
case OnTime
case Delayed(minutes: Int) //关联值:Int 参数名minutes可写可不写
init(){
//设置默认值
self = OnTime
}
var desc: String{
switch self{
case OnTime:
return "准时到达"
case .Delayed(let minutes):
return "延误了\(minutes)分钟"
}
}
}
var status = TrainStatus.Delayed(minutes: 50)
//=============================
//
// 逐级匹配
// 先匹配具体值,再匹配关联值
//
//=============================
switchstatus {
case .OnTime:
println("准时")
case .Delayed(minutes:1):
println("还行")
case .Delayed(2...10):
println("合理范围")
case .Delayed(minutes: _):
println("xxxx")
}
//输出xxxx
//更复杂和强大的栗子:
enum VacationStatus {
case Travelling(Status: TrainStatus)
case Relaxing(dayleft: Int)
}
var VStatus = VacationStatus.Travelling(Status: TrainStatus.Delayed(minutes: 100))
VStatus = VacationStatus.Relaxing(dayleft: 20)
switchVStatus {
case .Travelling(Status:.OnTime) :
println("OnTime)")
case .Travelling(Status:.Delayed(minutes: 1)) :
println("Delayed(minutes:1))")
case .Travelling(Status:.Delayed(2...15)) :
println("Delayed(2...15))")
case .Travelling(Status:.Delayed(_)) :
println("Delayed(_))")
case .Relaxing(dayleft: 1...30):
println("dayleft:1...30")
default :
println("default")
}
//=============================
//
// 类型模式匹配:
// 使用关键字as
//
//=============================
class Car {
var name = "Car"
func checkOil(){ println("this is a Car") }
}
class FormulaOne: Car{
var color = "red"
func enterPit(){ println("this is aFormulaOne") }
}
class raceCar: FormulaOne{
func turnTurbo(){ println("this is a raceCar") }
}
func turnUp(car: Car){
switch car {
case let f1 as FormulaOne:
f1.enterPit();
case let r1 as raceCar:
r1.turnTurbo();
fallthrough
default:
car.checkOil()
}
}
var mycar = raceCar() //mycar既满足FormulaOne也满足raceCar,所以优先匹配了FormulaOne
turnUp(mycar) //this is a raceCar
//=============================
//
// 元祖模式匹配:
// 支持元祖内逐项匹配
//
//=============================
let color = (1.0, 1.0, 1.0, 1.0)//该元祖表示RGB+Alpha
switchcolor {
case (0.0, 0.5...1.5, let blue, _):
println("Greenand \(blue * 100)% blue")
case let (r, g,b, 1.0) wherer==g && g==b:
println("....")
default:
println("****")
}
//=============================
//
// 模式匹配混合应用:
//
//=============================
import Foundation
struct State {
var name: NSString
var pop: NSNumber
var abbr: NSString
init(){
self.name = ""
self.pop = 100
self.abbr = ""
}
init(name: NSString,pop: NSNumber, abbr:NSString){
self.name = name
self.pop = pop
self.abbr = abbr
}
}
//未混合的情形,较复杂:
funcstateFromPlist0(list: Dictionary<String, AnyObject>) -> State? { //或者[String:AnyObject]
var name: NSString?
var pop: NSNumber?
var abbr: NSString?
switch list["name"]{//从key=name开始查找
case .Some(let listName asNSString): //.Some表示可选值是有值
name = listName
default:
name = nil
}
switch list["population"]{
case .Some(letlistPop as NSNumber):
pop = listPop
default:
pop = nil
}
switch list["abbr"]{
case .Some(letlistabbr as NSString):
abbr = listabbr
default:
abbr = nil
}
return State(name:name!, pop: pop!, abbr: abbr!)
}
//在commandline是可以通过的
let dict0 = ["name":"北京","population":2000_0000,"abbr":"京"]
let state0 = stateFromPlist0(dict0)
println("name=\(state0!.name),pop=\(state0!.pop),abbr=\(state0!.abbr)") //name=北京,pop=20000000,abbr=京
//模式匹配混合:
func stateFromPlist1(list: [String:AnyObject])-> State? {
switch (list["name"],list["population"],list["abbr"]){
case (
.Some(letlistName as NSString),
.Some(let listPop as NSNumber),
.Some(let listabbr as NSString))
where listName == "广州" && listabbr.length == 1:
return State(name:listName, pop: listPop, abbr: listabbr)
default:
return nil
}
}
stateFromPlist1(
[ "name":"广州",
"population":1300_0000,
"abbr":"粤"
])
println("name=\(state1!.name),pop=\(state1!.pop),abbr=\(state1!.abbr)") //name=广州,pop=13000000,abbr=粤