元组是多个数据类型组合在一起
举个例子🌰
let http404Error = (404, "Not Found")
print("The status code is \(http404Error.0)")
//获取元组的内部数据,通过位置索引
let value1 = http404Error.0 // 404
let value2 = http404Error.1 // "Not Found"
这些也是一个元组
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
print("The status message is \(statusMessage)")
//可以用_省略不需要的值
let (statusCode, _) = http404Error
print("The status code is \(statusCode)")
//可以像字典那样设置元组里面的值
let http404Error = (statusCode: 404, description: "Not Found")
print("The status code is \(http404Error.statusCode)")
网友评论