
TuplesTuples 可以用一对圆括号包裹若干个值,这些值的类型可以互不相同。例如,(404,"Not Found") 就可以表示一个http状态码.let http404Error = (404,"Not Found")http404Error 的类型是 (Int,String),他等于 (404,"Not Found")Tuples中可以包含任意类型你可以像下面这样赋值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"如果只需要Tuples中部分值,其他不需要的值可以用 _ 代替例如:let (justTheStatusCode,_) = http404Errorprint("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
https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309
以上是内存溢出为你收集整理的swift Tuples全部内容,希望文章能够帮你解决swift Tuples所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)