/**
switch匹配枚举
*/
enum Method {
case Add
case Sub
case Mul
case Div
}
func chooseMethod(op: Method) -> (Double,Double)-> Double {
switch op {
case .Add:
func add(a:Double,b:Double) -> Double {
return a + b
}
return add
case .Sub:
// 返回一个闭包
return { (a: Double,b: Double) -> Double in return a - b }
case .Mul:
// 返回一个闭包,省略闭包类型
return { return $0 * $1 }
case .Div:
// 返回一个闭包,return也省略掉
return { $0 / $1 }
default:
return { $0 + $1}
}
}
let val = chooseMethod(op: .Add)(30,5)
print(val)
原文链接:https://www.f2er.com/swift/322442.html