Swift学习笔记之基本数据类型-元组

1. 代码示例:

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

import Cocoa


//元组定义,里面可包含不同的数据类型
let tupe = (404, "Not Found", true, 0.01)



//将一个元组的内容分割常量或者变量
let http404Error = (404, "Not Found")
var(statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
print("The status message is \(statusMessage)\n")



//如果只需要元组中的部分值,忽略的部分可用下划线—替代
let (justStatusCode, _) = http404Error
print("The status code is \(statusCode)\n")


//可通过索引,获取元组的具体元素,索引从0开始
print(http404Error.1+"\n")


//可以将元组中的元素进行命名,并通过名字访问元素值
let http200Status = (statusCode:200, statusMessage: "OK")
print("The status code is \(http200Status.statusCode)")
print("The status message is \(http200Status.statusMessage)\n")


//方法的返回值也可以为元组
func getTupe() ->(Int, String){
    return (503, "服务器异常")
}

print(getTupe())
2. 运行结果

Swift学习笔记之基本数据类型-元组_第1张图片

你可能感兴趣的:(swift)