Swift

来源:互联网 发布:怎样免费开淘宝网店 编辑:程序博客网 时间:2024/05/16 12:20
元组(tuples)

把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。

下面这个例子中,(404, "Not Found") 是一个描述 HTTP 状态码(HTTP status code)的元组。HTTP 状态码是当请求网页的时候 web 服务器返回的一个特殊值。如果我们请求的网页不存在就会返回一个 404 Not Found 状态码。

let http404Error = (404, "Not Found")// http404Error 的类型是 (Int, String),值是 (404, "Not Found")
(404, "Not Found") 元组把一个 Int 值和一个 String 值组合起来表示 HTTP 状态码的两个部分:一个数字和一个人类可读的描述。这个元组可以被描述为“一个类型为 (Int, String) 的元组”。

我们可以把任意顺序的类型组合成一个元组,这个元组可以包含所有类型。只要你想,你可以创建一个类型为 (Int, Int, Int) 或者 (String, Bool) 或者其他任何你想要的组合的元组。

你可以将一个元组的内容分解(decompose)成单独的常量和变量,然后你就可以正常使用它们了:

let (statusCode, statusMessage) = http404Errorprint("The status code is \(statusCode)")// 输出 "The status code is 404"print("The status message is \(statusMessage)")// 输出 "The status message is Not Found"
如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记:

let (justTheStatusCode, _) = http404Errorprint("The status code is \(justTheStatusCode)")// 输出 "The status code is 404"
此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:

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"
作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个 (Int, String) 元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用。

注意:元组在临时组织值的时候很有用,但是并不适合创建复杂的数据结构。如果你的数据结构并不是临时使用,请使用类或者结构体而不是元组.

元组的使用 (Tuple)

比如交换输入,我们经常会这么写

func swap<T>(a: inout T, b: inout T) {    let temp = a    a = b    b = temp}
但是要是使用多元组的话,我们可以不使用额外空间就完成交换,编写简单:
func swap2<T>(a: inout T, b: inout T) {    (a,b) = (b,a)}
简单使用

var a = 3var b = 5swap(a: &a, b: &b)print(a,b) //5 3swap2(a: &a, b: &b)print(a,b) //3 5





原创粉丝点击