一:元组
定义:元组(tuples)把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。
//1:定义
//一个类型为 (Int, String) 的元组
// http404Error 的类型是 (Int, String),值是 (404, "Not Found")
let http404Eroor = (404,"Not Found")
print(http404Eroor.0)
print(http404Eroor.1)
//2:访问
//->全部元组元素
let (stateCode,stateMessage) = http404Eroor
print("The stateCode is \(stateCode)")
print("The stateMessage is \(stateMessage)")
//->部分元组元素
let (onlyTheStateCode,_)=http404Eroor
print("The stateCode is \(onlyTheStateCode)")
//-> 元组下标访问
print("The stateCode is \(http404Eroor.0)")
print("The stateMessage is \(http404Eroor.1)")
//定义并初始化赋值
let http200status = (statusCode:200,description:"OK")
print("The stateCode is \(http200status.statusCode)")
print("The stateCode is \(http200status.description)")
注意:
1:元组可以任意类型组合
2:元组在临时组织值的时候很有用,但是并不适合创建复杂的数据结构。如果你的数据结构并不是临时使用,请使用类或者结构体。
========一只程序媛!!=========