swift元组_08_swift元组基本使用

//: Playground - noun: a place where people can play

import UIKit

//1.元组的定义
//描述网络连接的状态 200-OK  404-NotFound 304-redirection
//(1)元组变量的定义
var httpError = (404, "NotFound")
print(httpError)

//(2)省略元组变量名,而是直接指定元组中各字段的名称
var (code, msg) = (304, "Redirection")
print(code)
print(msg)

//(3)显式指定元组的类型 类型注解
var httpOK : (code:Int, msg:String) = (200, "OK")

//(4)最简便的方式
var httpOK2 = (code:200, msg:"OK")
print(httpOK2.code)
print(httpOK2.msg)


//2.访问元组中成员(字段)
//(1)直接通过字段名访问

//(2)通过序号访问成员
print(httpOK2.0)
print(httpOK2.1)

var erroCode = httpOK2.0
var erroMsg = httpOK2.1

//(3)部分取值,其它字段不关注
let (errCode, _) = httpOK2
print(errCode)


//3.元组一般应用于函数中,当函数需要多个返回值时,使用元组
时光见证了成长,还很无知,我想一点点幼稚转为有知!
原文地址:https://www.cnblogs.com/foreveriOS/p/5558748.html