Swift编程入门(8)数组

2018/12/19 23:13 下午 posted in  Swift入门

编程中的一个重要任务是把逻辑相关的一组值放在一起。比如,想象你的应用要保存用户的好友列表、最爱的图书、旅行地点等。通常有必要具备将这些值放在一起并在代码中传递的能力。容器类型让这些操作变得方便。
Swift有一组容器类型,首先介绍的是数组(array)。
数组是值的有序集合。数组的每个位置都用索引标记,任何值都可以在数组中出现多次。数组通常用于值的顺序很重要或者很有用的场合,但是值的顺序是否有意义并不是先决条件。

[TOC]
##创建数组
创建数组有两种形式

import Cocoa
var bucketList: Array<String>

这里创建了一个名为bucketList的变量,类型是Array。大部分语法看起来应该都挺熟悉。比如,关键字var表示bucketList是变量。这意味着bucketList可变,所以我们可以修改这个数组。不可变数组同样存在,我们会在本章稍后讨论。
语法中的新东西是。这句代码告诉bucketList它应该接受什么样的实例。在这里,数组会接受String类型的实例。数组可以持有任何类型的实例。因为这个数组会持有与未来目标相关的信息,所以用String实例是合理的。

还有另外一种语法

import Cocoa 
var bucketList: [String]

方括号表示bucketList是Array实例,而String表示bucketList接受什么类型的值。
现在只是声明了bucketList,还没有初始化。这意味着它还没有准备好接受String类型的实例。如果你现在试着添加目标到bucketList中,会得到一个错误,说你在数组还未初始化bucketList时就添加实例。

初始化

import Cocoa 
var bucketList: [String] = ["Climb Mt. Everest"]

这里用到了赋值运算符=和数组字面量语法["Climb Mt. Everest"]。数组字面量是用任意其包含的实例初始化数组的快捷语法。

类型推断

import Cocoa 
var bucketList= ["Climb Mt. Everest"] 

访问和修改数组

添加

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
bucketList.append("Fly hot air balloon to Fiji") 
bucketList.append("Watch the Lord of the Rings trilogy in one day") 
bucketList.append("Go on a walkabout") 
bucketList.append("Scuba dive in the Great Blue Hole") 
bucketList.append("Find a triple rainbow") 

这里用到append(_:)给bucketList添加值。append(_:)方法接受一个参数,类型可以是数组接受的任何类型,并将其变为数组的新元素。

删除

用函数remove(at:)来删除,通过索引来删除。

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
bucketList.append("Fly hot air balloon to Fiji") 
bucketList.append("Watch the Lord of the Rings trilogy in one day") 
bucketList.append("Go on a walkabout") 
bucketList.append("Scuba dive in the Great Blue Hole") 
bucketList.append("Find a triple rainbow") 
bucketList.remove(at: 2)
bucketList

获取数组元素个数

用数组的count的属性查看

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
bucketList.append("Fly hot air balloon to Fiji") 
bucketList.append("Watch the Lord of the Rings trilogy in one day") 
bucketList.append("Go on a walkabout") 
bucketList.append("Scuba dive in the Great Blue Hole") 
bucketList.append("Find a triple rainbow") 
print(bucketList.count)

用下标获取值

import Cocoa
var bucketList = ["Climb Mt. Everest"]
bucketList.append("Fly hot air balloon to Fiji")
bucketList.append("Watch the Lord of the Rings trilogy in one day")
bucketList.append("Go on a walkabout")
bucketList.append("Scuba dive in the Great Blue Hole")
bucketList.append("Find a triple rainbow")
print(bucketList[0...2])

获取索引0到2三个值

用下标添加信息

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
bucketList.append("Fly hot air balloon to Fiji") 
bucketList.append("Watch the Lord of the Rings trilogy in one day") 
bucketList.append("Go on a walkabout") 
bucketList.append("Scuba dive in the Great Blue Hole") 
bucketList.append("Find a triple rainbow") 
bucketList[2] += " in Australia"

这里使用加法和赋值运算符+=来给索引2的元素增加文本

替换数组元素

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
bucketList.append("Fly hot air balloon to Fiji") 
bucketList.append("Watch the Lord of the Rings trilogy in one day") 
bucketList.append("Go on a walkabout") 
bucketList.append("Scuba dive in the Great Blue Hole") 
bucketList.append("Find a triple rainbow") 
bucketList[0] = "Climb Mt. Kilimanjaro"

用循环从一个数组添加元素到另一个数组

import Cocoa
var bucketList = ["Climb Mt. Everest"]
var newItems = [
    "Fly hot air balloon to Fiji",
    "Watch the Lord of the Rings trilogy in one day",
    "Go on a walkabout",
    "Scuba dive in the Great Blue Hole",
    "Find a triple rainbow"
]
for item in newItems {
    bucketList.append(item)
}
bucketList

用加法和赋值运算符重构代码

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
var newItems = [ 
    "Fly hot air balloon to Fiji", 
    "Watch the Lord of the Rings trilogy in one day", 
    "Go on a walkabout", 
    "Scuba dive in the Great Blue Hole", 
    "Find a triple rainbow" 
] 
bucketList += newItems
bucketList

插入新目标

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
var newItems = [ 
    "Fly hot air balloon to Fiji", 
    "Watch the Lord of the Rings trilogy in one day", 
    "Go on a walkabout", 
    "Scuba dive in the Great Blue Hole", 
    "Find a triple rainbow" 
] 
bucketList += newItems
bucketList.insert("Toboggan across Alaska", at: 2)

insert(_:at:)函数有两个参数。第一个参数接受要添加到数组的实例。(回忆一下,这个数组接受String实例。)第二个参数接受你想添加的元素在数组中位置的索引。

数组相等

==检查是否相等

import Cocoa 
var bucketList = ["Climb Mt. Everest"] 
var newItems = [ 
"Fly hot air balloon to Fiji", 
"Watch the Lord of the Rings trilogy in one day", 
"Go on a walkabout", 
"Scuba dive in the Great Blue Hole", 
"Find a triple rainbow" 
] 
bucketList += newItems 
bucketList 
bucketList.remove(at: 2) 
print(bucketList.count) 
print(bucketList[0...2]) 
bucketList[2] += " in Australia" 
bucketList[0] = "Climb Mt. Kilimanjaro" 
bucketList.insert("Toboggan across Alaska", at: 2) 
bucketList 
var myronsList = [
"Climb Mt. Kilimanjaro",
"Fly hot air balloon to Fiji",
"Toboggan across Alaska",
"Go on a walkabout in Australia",
"Find a triple rainbow",
"Scuba dive in the Great Blue Hole"
] 
let equal = (bucketList == myronsList)

equal却被判定为假。为什么?
记住,数组是有序的。这意味着两个内容一样的数组如果顺序不同也会被认为是不相等的。在这个例子中,myronsList给"Find a triple rainbow"顺序不一样。

不可变数组

你花了很多精力来对目标清单数组进行小修小补,但是也可以创建一个不能修改的数组,也就是不可变数组(immutable array)

let lunches = [
"Cheeseburger",
"Veggie Pizza",
"Chicken Caesar Salad",
"Black Bean Burrito",
"Falafel Wrap"
]

使用let关键字创建不可变数组。如果试图以任何方式修改数组,编译器会报错,提示你不能修改不可变数组。如果试图重新赋一个新数组给lunches,编译器会报错,提示无法给一个用let关键字创建的常量重新赋值。