print("The status code is \(statusCode)")// 输出 "The status code is 404"
print("The status message is \(statusMessage)") // 输出 "The status message is Not Found"
如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线( _ )标记:
(Int, I
let (justTheStatusCode, _) = http404Error print("The status code is \(justTheStatusCode)") // 输出 "The status code is 404”
而且你还可以像访问数组那样取得第零个位置以及其他位置的值是神马(下标是从0开始的哦)
print("The status code is \(http404Error.0)")// 输出 "The status code is 404"print("The status message is \(http404Error.1)") // 输出 "The status message is Not Found"
当然了你还可以在定义元祖的时候给单个元素进行命名:
let http200Status = (statusCode: 200, description: "OK")
给元组中的元素命名后,你可以通过名字来获取这些元素的值:
print("The status code is \(http200Status.statusCode)")
// 输出 "The status code is 200"
print("The status message is \(http200Status.description)") // 输出 "The status message is OK"