// 元组 元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。-----------
let tup = (88,"eqi",["dds"])
var ss = tup.0
var tup2 = (name:"mac", age: 88,["dds"])
var name = tup2.0 // 或者
var name1 = tup2.name
// 分解
var tp = (404,"not found")
let (code, status) = tp
let (code2,_) = tp // _忽略值
func format(tup:(String, Int)) -> String {
return "Hi,\(tup.0),\(tup.1)"
}
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
println("(0, 0) is at the origin")
case (_, 0):
println("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
println("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
println("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
println("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
println("on the x-axis with an x value of \(x)")
case (0, let y):
println("on the y-axis with a y value of \(y)")
case let (x, y):
println("somewhere else at (\(x), \(y))")
}
// case 分支的模式可以使用where语句来判断额外的条件。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
println("(\(x), \(y)) is just some arbitrary point")
}
// 输出 "(1, -1) is on the line x == -y"