Swift学习之六:元组(Tuples)
2016-08-23 16:08
316 查看
元组是多个值组合而成的复合值。元组中的值可以是任意类型,而且每一个元素的类型可以是不同的。
如:
[objc] view
plain copy
print?
// http404Error是(Int, String)类型
// 这个元组是二元组,是一个整型和字符串类型的组合,这里代表着404的意思是Not Found
let http404Error = (404, "Not Found")
元组是可以分解的。
如:
[objc] view
plain copy
print?
let (statusCode, statusMessage) = http404Error
// prints "The status code is 404"
println("The status code is \(statusCode)")
println("The status message is \(statusMessage)")
元组在分解时,可以只分解部分值,而忽略其它值,忽略其它值可以用下划线_来代替.
如:
[objc] view
plain copy
print?
let (statusCode, _) = http404Error
println("The status code is \(statusCode)")
访问元组的另外一种方式:通过下标获取,从0开始
如:
[objc] view
plain copy
print?
let http404Error = (404, "Not Found")
// 通过元组.0获取第二个值
println("The status code is \(http404Error.0)")
// 通过元组.1获取第二个值
println("The status message is \(http404Error.1)")
访问元组元素还有一种方式:给定元组元素命名,然后通过名称获取值(元组.名称)
如:
[objc] view
plain copy
print?
let http200Status = (statusCode: 200, description: "OK")
println("The status code is \(http200Status.statusCode)")
println("The status message is \(http200Status.description)")
如:
[objc] view
plain copy
print?
// http404Error是(Int, String)类型
// 这个元组是二元组,是一个整型和字符串类型的组合,这里代表着404的意思是Not Found
let http404Error = (404, "Not Found")
元组是可以分解的。
如:
[objc] view
plain copy
print?
let (statusCode, statusMessage) = http404Error
// prints "The status code is 404"
println("The status code is \(statusCode)")
println("The status message is \(statusMessage)")
元组在分解时,可以只分解部分值,而忽略其它值,忽略其它值可以用下划线_来代替.
如:
[objc] view
plain copy
print?
let (statusCode, _) = http404Error
println("The status code is \(statusCode)")
访问元组的另外一种方式:通过下标获取,从0开始
如:
[objc] view
plain copy
print?
let http404Error = (404, "Not Found")
// 通过元组.0获取第二个值
println("The status code is \(http404Error.0)")
// 通过元组.1获取第二个值
println("The status message is \(http404Error.1)")
访问元组元素还有一种方式:给定元组元素命名,然后通过名称获取值(元组.名称)
如:
[objc] view
plain copy
print?
let http200Status = (statusCode: 200, description: "OK")
println("The status code is \(http200Status.statusCode)")
println("The status message is \(http200Status.description)")
相关文章推荐
- swift学习记录(元组tuples)
- Swift学习笔记之元组(tuples)
- Swift学习Day03(元组 Tuples类型)
- Swift学习笔记(2)--元组(Tuples)、Optional(可选值)、(Assertions)断言
- Swift学习Day03(元组 Tuples类型)
- Swift学习之六:元组(Tuples)
- Swift学习之六:元组(Tuples)
- Swift学习笔记(2)--元组(Tuples)、Optional(可选值)、(Assertions)断言
- swift学习2 元组 tuples
- swift学习02-元组tuple
- swift tuples 元组(二)【转自http://blog.csdn.net/zhang_biao_1991/article/details/31344495】
- [Swift]元组-Tuples
- Glib学习(10) 关系和元组 Relations and Tuples
- iOS开发46-iOS Swift元组(Tuples)
- Swift编程语言学习1.5——类型别名、布尔值、元组
- Swift学习笔记之基本数据类型-元组
- swift 学习计划(一)元组、Optional可选类型
- 02-Swift学习笔记-元组类型
- Swift 元组(Tuples)介绍
- Swift学习——格式控制符和元组流程控制(二)