array初始化和拼接
创建
var shopNumList = [Int]()
var shopNumList2:[Int] = []
var shopNumList3:Array<Int> = []
var shopNumList4 = Array<Int>()
print("someInts is of type [Int] with \(shopNumList.count) items.")
打印 "someInts is of type [Int] with 0 items."
拼接
shopNumList.append(3)
someInts 现在包含一个 Int 值
shopNumList = []
someInts 现在是空数组,但是仍然是 [Int] 类型的。
var threeDoubles = Array(repeating: 0.0, count: 3)
threeDoubles 是一种 [Double] 数组,等价于 [0.0, 0.0, 0.0],repreting:初始值
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
anotherThreeDoubles 被推断为 [Double],等价于 [2.5, 2.5, 2.5]
组合拼接
var sixDoubles = threeDoubles + anotherThreeDoubles
sixDoubles 被推断为 [Double],等价于 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
根据字面量直接创建
var shoppingList: [String] = ["Eggs", "Milk","hehe"] //等于 var shoppingList = ["Eggs", "Milk"]
shoppingList 已经被构造并且拥有两个初始项。
使用加法赋值运算符(+=)也可以直接在数组后面添加一个或多个拥有相同类型的数据项:
shoppingList += ["Baking Powder"]
shoppingList 现在有四项了
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
shoppingList 现在有6项了
array访问和修改
shoppingList = ["Eggs", "Milk"]
print("The shopping list contains \(shoppingList.count) items.")
输出 "The shopping list contains 2 items."(这个数组有2个项)
使用布尔属性isEmpty作为一个缩写形式去检查count属性是否为0:
if shoppingList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
打印 "The shopping list is not empty."(shoppinglist 不是空的)
调用数组的insert(_:at:)方法来在某个具体索引值之前添加数据项:
shoppingList.insert("Maple Syrup", at: 0)
print("start array访问和修改")
print(shoppingList)
shoppingList 现在有3项
"Maple Syrup" 现在是这个列表中的第一项
let mapleSyrup = shoppingList.remove(at: 0)
print(shoppingList)
print(mapleSyrup)
索引值为0的数据项被移除
shoppingList 现在只有2项,而且不包括 Maple Syrup
mapleSyrup 常量的值等于被移除数据项的值 "Maple Syrup"
let Milk = shoppingList.removeLast()
print(shoppingList)
print(Milk)
print("end array访问和修改")
数组的最后一项被移除了
shoppingList 现在只有1项,不包括 Milk
Milk 常量的值现在等于 "Milk" 字符串
获取最小,最大值
shoppingList.insert("hans", at: 0)
shoppingList.insert("whaps", at: 0)
shoppingList.insert("apple", at: 0)
shoppingList.min() //1
shoppingList.max() //U
包含
shoppingList.contains("apple")
shoppingList.contains("hehe")
array遍历
for item in shoppingList {
print(item)
}
for (index, value) in shoppingList.enumerated() {
print("Item \(String(index + 1)): \(value)")
}
dictionary使用
var namesOfIntegers = [Int: String]()
namesOfIntegers 是一个空的 [Int: String] 字典
namesOfIntegers[16] = "sixteen"
namesOfIntegers 现在包含一个键值对
namesOfIntegers = [:]
namesOfIntegers 又成为了一个 [Int: String] 类型的空字典
根据字面量直接创建
var airports1: [String: String] = ["airports1": "ch999", "airports2": "999ch"]
var airports2 = ["airports1": "ch999", "airports2": "999ch"]
访问和修改字典
也可以用count
print("The dictionary of airports contains \(airports1.count) items.")
打印 "The dictionary of airports contains 2 items."(这个字典有两个数据项)
使用布尔属性isEmpty作为一个缩写形式去检查count属性是否为0:
if airports1.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
打印 "The airports dictionary is not empty."
除了直接用下表,字典的updateValue(_:forKey:)方法可以设置或者更新特定键对应的值。updateValue(_:forKey:)方法在这个键不存在对应值的时候会设置新值或者在存在时更新已存在的值。和下标方法不同的是,updateValue(_:forKey:)这个方法返回更新值之前的原值。这样使得我们可以检查更新是否成功。
updateValue(_:forKey:)方法会返回对应值的类型的可选值。举例来说:对于存储String值的字典,这个函数会返回一个String?或者“可选 String”类型的值。
如果有值存在于更新前,则这个可选值包含了旧值,否则它将会是nil。
if let oldValue = airports1.updateValue("hans", forKey: "airports1") {
print("The old value for DUB was \(oldValue).")
}
输出 "The old value for DUB was Dublin."
dictionary遍历
for (airportCode, airportName) in airports1 {
print("\(airportCode): \(airportName)")
}
for airportCode in airports1.keys {
print("Airport code: \(airportCode)")
print("Airport code: \(airports1[airportCode])")
}
for airportName in airports1.values {
print("Airport name: \(airportName)")
}
如果我们只是需要使用某个字典的键集合或者值集合来作为某个接受Array实例的 API 的参数,可以直接使用keys或者values属性构造一个新数组:
let airportCodes = [String](airports1.keys)
airportCodes 是 ["airports1", "airports2"]
let airportNames = [String](airports1.values)
airportNames 是 ["hans", "999ch"]