Swift基础---Tuples

2018-09-06 16:42:23 浏览数 (1)

声明

代码语言:javascript复制
let http404Error = (404, "Not Found")
// http404Error is of type (Int, String),
// and equals (404, "Not Found")

使用

代码语言:javascript复制
let (statusCode, statusMessage) = http404Error
println("The status code is (statusCode)")
// prints "The status code is 404"

println("The status message is (statusMessage)")
// prints "The status message is Not Found"

忽略不用的值

代码语言:javascript复制
let (justTheStatusCode, _) = http404Error
println("The status code is (justTheStatusCode)")
// prints "The status code is 404"

通过索引访问

代码语言:javascript复制
println("The status code is (http404Error.0)")
// prints "The status code is 404"

println("The status message is (http404Error.1)")
// prints "The status message is Not Found"

指定Tuples的名字

代码语言:javascript复制
let http200Status = (statusCode: 200, description: "OK")

println("The status code is (http200Status.statusCode)")
// prints "The status code is 200"

println("The status message is (http200Status.description)")
// prints "The status message is OK"

0 人点赞