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

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 房客

用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-17 · via 博客园 - 房客

基本的数组结构Array:

var shoppingList: String[] = ["Eggs", "Milk"]

这个shoppingList和传统意义上的数组是没区别的。但它却又可以这样:

println("The shopping list contains \(shoppingList.count) items.")

shoppingList.count的这个count是T(继承自ArrayType)的属性,可以获取当前数组的长度。

同时,shoppingList还可以通过 append(只能添加单个元素)或者+=(可添加一个或者多个元素)运算符添加新元素。

 var shoppingList: String[] = ["Eggs", "Milk"]
        shoppingList.append("Banana")
        shoppingList += "Apple"
        shoppingList += ["WaterMelon","Tomato"]

也可以直接通过下标来替换一个或者多个元素

        var shoppingList: String[] = ["Eggs", "Milk"]
        shoppingList.append("Bananas")
        shoppingList += "Apples"
        shoppingList += ["WaterMelons","Tomatos","Chocolate"]
        shoppingList += ["",""]
        shoppingList[7...8] = ["Cola","Cookies"]

可以通过下标来获取指定位置的一个或者多个元素

var firstElement = shoppingList[0]
var mulElements = shoppingList[4...6]

在指定位置插入新元素

shoppingList.insert("Cheese",atIndex: 4)
        for item in shoppingList{
            println(item);
        }

移除元素有2种方法,1,直接移除数组最后一个元素。 2,移除指定下标位置的元素

        shoppingList.removeLast()
        for item in shoppingList{
            println(item);
        }
        shoppingList.removeAtIndex(0)
        for item in shoppingList{
            println(item);
        }

如果想移除数组中的某个指定的元素,没有直接的方法。只能遍历并移除:

        for (index,value) in enumerate(shoppingList){
            println("Item \(index+1): \(value)")
            if(value == "Bananas"){
                shoppingList.removeAtIndex(index);
            }
        }

其实这个方法按微软的说法的话,是不够严谨的。removeAtIndex方法执行后,返回值是一个T类型。应该是shoppingList的一个copy,应该是不可以影响到当前操作的数组的。我跟踪了一下,事实上这个方法目前是会影响到当前的数组的。

数组初始化

可以直接初始化指定数据类型的空数组,留着后面再填充数据。

var someInts = Int[]()
println("someInts is of type Int[] with \(someInts.count) items.")

可看到输出的结果为0个items在数组中。

也可以直接指定大小的空数组,同时指定初始化数据:

var threeDoubles = Double[](count:3, repeatedValue:0.0)
println("threeDoubles is of type Double[] with \(threeDoubles.count) items")

可以看到,当前数组中有3个值都是0.0的item. 这个跟T类型的构造函数有关:

init(count: Int, repeatedValue: T)

总结:

1)swift的数组是通过[]来定义数据类型,()来指定数据的。

2)可以通过insert,append和+=操作符添加数组

3)也可以通过构造函数init(count: Int, repeatedValue: T)来初始化指定大小和初始值的数组

4)可以通过removeLast和removeAtIndex来移除指定的item

5)可以通过下标来获取或者设置指定的item

        var shoppingList: String[] = ["Eggs", "Milk"]
        shoppingList.append("Bananas")
        shoppingList += "Apples"
        shoppingList += ["WaterMelons","Tomatos","Chocolate"]
        shoppingList += ["",""]
        shoppingList[7...8] = ["Cola","Cookies"]
        var firstElement = shoppingList[0]
        var mulElements = shoppingList[4...6]
        shoppingList.insert("Cheese",atIndex: 4)
//        for item in shoppingList{
//            println(item);
//        }
        shoppingList.removeLast()
//        for item in shoppingList{
//            println(item);
//        }
        shoppingList.removeAtIndex(0)
//        for item in shoppingList{
//            println(item);
//        }
        for (index,value) in enumerate(shoppingList){
            println("Item \(index+1): \(value)")
            if(value == "Bananas"){
                shoppingList.removeAtIndex(index);
            }
        }
        
        var someInts = Int[]()
        println("someInts is of type Int[] with \(someInts.count) items.")
        
        var threeDoubles = Double[](count:3, repeatedValue:0.0)
        println("threeDoubles is of type Double[] with \(threeDoubles.count) items")


        //println("The fruits are \(mulElements.count) at position 4-6 in shopping list")
        //println("The shopping contains \(shoppingList.count) items")