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. 运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值