swift Tuples
发布时间:2020-12-14 06:41:20 所属栏目:百科 来源:网络整理
导读:TuplesTuples 可以用一对圆括号包裹若干个值,这些值的类型可以互不相同。例如,( 404 , "Not Found" ) 就可以表示一个HTTP状态码. let http404Error = ( 404 , "Not Found" )http404Error 的类型是 (Int,String),他等于 ( 404 , "Not Found" )Tuples中可以包
Tuples
Tuples 可以用一对圆括号包裹若干个值,这些值的类型可以互不相同。
例如,(404,"Not Found") 就可以表示一个HTTP状态码.
let http404Error = (404,"Not Found")
http404Error 的类型是 (Int,String),他等于 (404,"Not Found")
Tuples中可以包含任意类型
你可以像下面这样赋值
let (statusCode,statusMessage) = http404Error
print("The status code is (statusCode)")
// 输出 "The status code is 404"
print("The status message is (statusMessage)")
// 输出 "The status message is Not Found"
如果只需要Tuples中部分值,其他不需要的值可以用 _ 代替
例如:
let (justTheStatusCode,_) = http404Error
print("The status code is (justTheStatusCode)")
// 输出 "The status code is 404"
你也可以通过下标来获取值,下标从0开始
例如:
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"
你可以给Tuple中个元素取个名字
例如:
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"
Tuples 可以作为函数的值返回
原文 Tuples (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 【Visual Basic】列表控件ListView的增删改查、模态对话框、
- ruby-on-rails – 在heroku回滚后部署
- ruby-on-rails – Rails 3和Rspec:计数器缓存列在预期时更
- string – VB脚本日期格式“YYYYMMDDHHMMSS”
- 对于(U)Int8 / 16/32/64类型,是否可以将Swifts自动数值桥接
- ruby-on-rails – Devise with Confirmable – 当用户尝试使
- c# – Windows 8 Metro App进行设置弹出窗口
- 用Flex3.0导入代码
- ruby – 在Centos 6.2上使用RVM 1.9.3安装nokogiri时出现问
- swift开发iOS应用之label控件与代码关联