Swift语法_元组

一:元组
定义:元组(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:元组在临时组织值的时候很有用,但是并不适合创建复杂的数据结构。如果你的数据结构并不是临时使用,请使用类或者结构体。

========一只程序媛!!=========

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值