定义一个Tuple
let color = (1.0,1.0,1.0,1.0)
switch color{
case (0.0,0.5...1.0,let blue,_): //匹配第一个值为0.0第二个值为0.5到1.0第三四值为任意,并将第三个值传给blue变量
println("Blue is \(blue)")
case let (r,g,b,1.0) where r == g && g == b: //额外条件where 本个亦是的值要相等
println("Opaque grey \(r * 100)%")
default:
println("unkonw")
}
元祖就说完了,接下来定义一个Struct
struct Color {
var red:Double?
var green:Double?
var blue:Double?
var alpha:Double?
init(red:Double,green:Double,blue:Double,alpha:Double){
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
}
var mycolor = Color(red: 10, green: 20, blue: 60, alpha: 1.0)
switch mycolor{
case let a where a.red == 10 && a.blue == 60:
println("yes")
default:
println("unkonw")
}
时间: 2024-10-27 07:21:32