惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 房客

用JavaDoc生成项目文档 thymeleaf参考手册 转的一个Java基本功 杂记 修改Esxi克隆的CentOS的IP地址 CentOS搭建socket5代理服务器 CentOS上搭建Nginx + Mono 运行 asp.net 启动PPT的时候一直配置vs2013的问题解决 swift 集合类型(二) swift 集合类型(一) swift 定时器的使用 swift UIImage加载远程图片和圆角矩形 swift 随机生成背景颜色 VSFTPD配置TLS/SSL JayProxy的设置 CentOS 安装配置 PPTP VPN 服务器 - 房客 Nginx + fastcgi 处理php 编译安装php5.3.8(含php-fpm) CentOS 安装eaccelerator PHP加速
swift 元组
房客 · 2014-06-18 · via 博客园 - 房客

在swift里面有这么个好玩的东西 - 元组(tuples). 

它可以包含不同的数据类型组成一个复合值,同时也可以像json对象或者dictionary一样,指定键值。同时也可以像属性一样获取键值。

见识一下:

let http404Error = (404, "Not Found")
let http200Status = (statusCode:200,description:"OK")

2种初始方式,可以直接将值组合,也可以按键值对组合。

如果是值组成的复合值获取时,可以:

        let http404Error = (404, "Not Found")
        
        let (statusCode,statusMessage) = http404Error
        
        println("Code: \(statusCode) \t Message: \(statusMessage)")

也可以:

let http404Error = (404, "Not Found")
println("Code: \(http404Error.0) \t Message: \(http404Error.1)")

如果是按键值对组成的复合值,就可以按属性的形式访问来获取相关值:

        let http200Status = (statusCode:200,description:"OK")
        
        println("Code: \(http200Status.statusCode) \t Message: \(http200Status.description)")

总结:

1)元组在某些时候非常有用,但它毕竟是个临时复合值。按官方:

作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个(Int, String)元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用。

元组在临时组织值的时候很有用,但是并不适合创建复杂的数据结构。如果你的数据结构并不是临时使用,请使用类或者结构体而不是元组。

2)元组在初始化时,使用的是(),而非其它的<>(Dictionary的类型声明需要使用<>)或者[](数组Array或者Dictionary构造时需要使用[])

3)元组是可以直接保存多种数据类型的数据,并组成一个复合值的。这个是最显著的一个特征,也是非常有帮助的。