Array 數(shù)組 ?相當(dāng)于OC中的可變數(shù)組
var someInts = [Int]() //初始化? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? someInts.append(3) // 添加一個元素? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? someInts= [] // 制空數(shù)組?
var threeDoubles = Array(repeating:0.0,count:3) // 初始化數(shù)組? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var anotherThreeDoubles = Array(repeating:2.5,count:3) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var sixDoubles = threeDoubles + anotherThreeDoubles // 合并兩個數(shù)組 ( 相當(dāng)于 第一個數(shù)組 依次添加第二個數(shù)組里面的元素)
var shoppingList: [String] = ["Eggs","Milk"] //用字面量形式 初始化數(shù)組? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var shoppingList = ["Eggs","Milk"] ? ? ? shoppingList[4...6] = ["Bananas","Apples"]//改變一定范圍內(nèi)的元素? ? shoppingList.insert("Maple Syrup",at:0)//插入元素 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? let mapleSyrup = shoppingList.remove(at:0) //移除元素
for (index,value) in shoppingList.enumerated() {//獲取數(shù)組元素以及對應(yīng)的索引
? ? ? ?print("Item\(index+1):\(value)")
}
Sets 集合
var letters = Set<Character>() //初始化集合? letters.insert("a")//插入元素 letters = [] // 制空集合? var favoriteGenres:Set<String> = ["Rock","Classical","Hip hop"] // 字面量初始化? ? ? ? ? ? ? var favoriteGenres:Set= ["Rock","Classical","Hip hop"] // 自行推斷是字符串類型的集合? ? ? ? ? favoriteGenres.isEmpty// 判空 favoriteGenres.insert("Jazz") // 插入元素 favoriteGenres.remove("Rock") // 刪除元素 favoriteGenres.contains("Funk")//包含
for genre in favoriteGenres.sorted() { //按照索引排序 和數(shù)組一樣
? ? ? print("\(genre)")
}
a.intersection(b)// 求a和b的交集? a.symmetricDifference(b)// a和b的非交集 a.union(b) // a和b的并集 a.subtracting(b) // a中除了b的元素剩下的
let houseAnimals:Set= ["??","??"]
let farmAnimals:Set= ["??","??","??","??","??"]
let cityAnimals:Set= ["??","??"]
houseAnimals.isSubset(of:farmAnimals) //是否是子集 ? ?// true
farmAnimals.isSuperset(of:houseAnimals) //是否包含 ? ?// true
farmAnimals.isDisjoint(with:cityAnimals) // 是否有交集 ? ??// true
Dictionary 字典
var namesOfIntegers = [Int:String]() //字典初始化 // namesOfIntegers is an empty [Int: String] dictionary? [ key : value ]? ? namesOfIntegers[16] ="sixteen" // namesOfIntegers now contains 1 key-value pair? ? namesOfIntegers= [:] // namesOfIntegers is once again an empty dictionary of type [Int: String]? var airports: [String:String] = ["YYZ":"Toronto Pearson","DUB":"Dublin"] //字典初始化 var airports= ["YYZ":"Toronto Pearson","DUB":"Dublin"] //和上句一樣 自行推斷類型 ?
if airports.isEmpty { // 判空
? ? print("The airports dictionary is empty.")
}else{
? ? print("The airports dictionary is not empty.")
} ? ?// Prints "The airports dictionary is not empty."
airports["LHR"] ="London" // 訪問值
if let oldValue = airports.updateValue("Dublin Airport",forKey:"DUB") { // 更換對應(yīng)key 的 value
? ? ? print("The old value for DUB was\(oldValue).")
} ? ?// Prints "The old value for DUB was Dublin."
if let removedValue = airports.removeValue(forKey:"DUB") { // 移除對應(yīng)key的value
? ? ? ?print("The removed airport's name is\(removedValue).")
}else{
? ? ? ?print("The airports dictionary does not contain a value for DUB.")
} ? ?// Prints "The removed airport's name is Dublin Airport."
for (airportCode,airportName) in ?airports{ // 遍歷字典
? ? ? ? print("\(airportCode):\(airportName)")
} ? ??// YYZ: Toronto Pearson ? ? ??// LHR: London Heathrow
for airportCode in airports.keys { // 遍歷字典的所有key
? ? print("Airport code:\(airportCode)")
} ? ?// Airport code: YYZ ? ? ? ?// Airport code: LHR
for airportName in airports.values{ // 遍歷字典所有的value
? ? ?print("Airport name:\(airportName)")
} ? ??// Airport name: Toronto Pearson ? ? ? ?// Airport name: London Heathrow
let airportCodes = [String](airports.keys) // ?獲取字典的所有的key 返回一個數(shù)組
// airportCodes is ["YYZ", "LHR"]
let airportNames = [String](airports.values) // 獲取字典所有的value 返回一個數(shù)組
// airportNames is ["Toronto Pearson", "London Heathrow"]