目录
数组(Array)
创建数组
//创建一个空数组
var someInts = [Int]()
someInts = []
//创建一个带默认值的数组
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles 是一种 [Double] 数组,等价于 [0.0, 0.0, 0.0]
//通过两个数组相加创建一个数组
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles 被推断为 [Double],等价于 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
//用数组字面量构建数组
var shoppingList: [String] = ["Eggs", "Milk"]
//或者
var shoppingList = ["Eggs", "Milk"]
// shoppingList 已经被构造并且拥有两个初始项。
访问、修改数组
var shoppingList = ["Eggs", "Milk"]
// 访问数组长度
shoppingList.count
// 判断数组是否为空
if shoppingList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
// 打印“The shopping list is not empty.”(shoppinglist 不是空的)
添加数组
1、使用append()方法在数组后面添加新元素
shoppingList.append("Flour")
2、使用加法赋值运算符(+=
)直接将另一个相同类型数组中的数据添加到该数组后面:
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
3、直接使用下标
var firstItem = shoppingList[0]
// 第一项是“Eggs”
shoppingList[0] = "Six eggs"
// 其中的第一项现在是“Six eggs”而不是“Eggs”
//利用下标来一次改变一系列数据值,即使新数据和原有数据的数量是不一样的
//下面的例子把 "Chocolate Spread"、"Cheese" 和 "Butter" 替换为 "Bananas" 和 "Apples":即三个元素变成两个元素
shoppingList[4...6] = ["Bananas", "Apples"]
4、使用insert(_:at:_)方法在某位置之前添加数据
shoppingList.insert("Maple Syrup", at: 0)
// 现在是这个列表中的第一项是“Maple Syrup”
移除数组
1、使用 remove(at:)
方法来移除数组中的某一项。
这个方法把数组在特定索引值中存储的数据项移除并且返回这个被移除的数据项(不需要的时候就可以无视它):
let mapleSyrup = shoppingList.remove(at: 0)
2、移除最后一项,也会返回被移除的数据项
let apples = shoppingList.removeLast()
数值的遍历
1、for-in循环
for item in shoppingList {
print(item)
}
2、需要每个数据项的值和索引值,可以使用 enumerated()
方法来进行数组遍历
for (index, value) in shoppingList.enumerated() {
print("Item \(String(index + 1)): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas
字典(Dictionary)
创建字典
//空字典
var namesOfIntegers = [Int: String]()
namesOfIntegers = [:]
//单个字典赋值
namesOfIntegers[16] = "sixteen"
//用字典字面量创建字典
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
//或者
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
访问、修改字典
// count 来获取字典的数据项数量
airports.count
// 判断是否为空(count 是否为0 )
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
添加字典
1、通过下标语法来给字典添加新的数据项
airports["LHR"] = "London"
// airports 字典现在有三个数据项
2、通过下标语法来修改特定键对应的值
airports["LHR"] = "London Heathrow"
3、通过updateValue(_:forKey:)
方法修改对应值
此方法会返回对应值类型的可选类型:(即,返回 对应值未修改之前的数据)
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
if let oldValue = airports.updateValue("Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
//打印:The old value for DUB was Dublin.
4、通过下标语法来在字典中检索特定键对应的值:
如果有对应的值,返回对应值的可选类型,否则返回nil
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
} else {
print("That airport is not in the airports dictionary.")
}
// 打印“The name of the airport is Dublin Airport.”
移除字典
1、移除一个键值对:使用下标语赋值nil
airports["APL"] = nil
// APL 现在被移除了
2、使用removeValue(forKey:)
方法移除键值对(如果键值对存在,移除键值对同时返回该键值,不存在则返回nil):
if let removedValue = airports.removeValue(forKey: "DUB") {
print("The removed airport's name is \(removedValue).")
} else {
print("The airports dictionary does not contain a value for DUB.")
}
// 打印“The removed airport's name is Dublin Airport.”
字典遍历
1、使用 for-in
循环来遍历某个字典中的键值对。
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
2、通过访问 keys
或者 values
属性,你也可以遍历字典的键或者值:
for airportCode in airports.keys {
print("Airport code: \(airportCode)")
}
for airportName in airports.values {
print("Airport name: \(airportName)")
}
3、使用 keys
或者 values
属性构造一个新数组:
let airportCodes = [String](airports.keys)
// airportCodes 是 ["YYZ", "LHR"]
let airportNames = [String](airports.values)
// airportNames 是 ["Toronto Pearson", "London Heathrow"]
4、Swift 的 Dictionary
是无序集合类型。为了以特定的顺序遍历字典的键或值,可以对字典的 keys
或 values
属性使用 sorted()
方法。