數組數組
1.建立一個數組app
var someInts = [Int]()空數組spa
someInts = []清空3d
var threeDoubles = Array(repeating: 0.0, count: 3)有默認值的數組blog
var shoppingList: [String] = ["Eggs", "Milk"]three
var arra = arrb + arrc建立一個數組是另兩個數組的相加ip
2.array.countrem
3.array.isEmptyit
4.加元素io
array.append("Flour")
array += ["Baking Powder"]
5.插入
array.insert("Maple Syrup", at: 0)
6.刪除
let a = array.remove(at: 0)
7.遍歷
for item in array {
print(item)
}
for (index, value) in array.enumerated() {
print("Item \(index + 1): \(value)")
}
集合
1.建立一個新的集合
var letters = Set<Character>()
letters = []清空
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
2.favoriteGenres.count
3.favoriteGenres.isEmpty
4.操做
favoriteGenres.insert("Jazz")
favoriteGenres.remove("Rock")
favoriteGenres.contains("Funk")是否存在
5.遍歷
for genre in favoriteGenres {
print("\(genre)")
}
for genre in favoriteGenres.sorted() {//按從小到大遍歷
print("\(genre)")
}
6.集合關係
intersect()兩個集合中都包含的值建立的一個新的集合。
exclusiveOr()只在一個集合中但不在兩個集合中的值建立一個新的集合。
union()兩個集合的值建立一個新的集合。
subtract()不在該集合中的值建立一個新的集合。
7.集合關係
isSubset(Of)判斷一個集合中的值是否也被包含在另一個集合中。
isSuperset(Of)判斷一個集合中包含的值是否含有另外一個集合中全部的值。
isStrictSubset(Of)或者isStrictSuperset(Of)判斷一個集合是不是另一個集合的子集合或者父集合而且和特定集合不相等。
isDisjoint(With)判斷兩個集合是否不含有相同的值。
字典
1.建立一個新的字典
var namesOfIntegers = [Int: String]()
namesOfIntegers[16] = "sixteen"若是沒有key就加,若是有key就覆蓋原來的值
namesOfIntegers = [:]清空
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
2.airports.count
3.airports.isEmpty
4.操做
let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB")更新
airports["APL"] = nil刪除
let removedValue = airports.removeValue(forKey: "DUB")刪除
5.遍歷
for (airportCode, airportName) in airports {
}
for airportCode in airports.keys {
}
for airportName in airports.values {
}
let airportCodes = [String](airports.keys)
let airportNames = [String](airports.values)