Array 数组 相当于OC中的可变数组
var someInts = [Int]() //初始化 someInts.append(3) // 添加一个元素 someInts= [] // 制空数组
var threeDoubles = Array(repeating:0.0,count:3) // 初始化数组 var anotherThreeDoubles = Array(repeating:2.5,count:3) var sixDoubles = threeDoubles + anotherThreeDoubles // 合并两个数组 ( 相当于 第一个数组 依次添加第二个数组里面的元素)
var shoppingList: [String] = ["Eggs","Milk"] //用字面量形式 初始化数组 var shoppingList = ["Eggs","Milk"] shoppingList[4...6] = ["Bananas","Apples"]//改变一定范围内的元素 shoppingList.insert("Maple Syrup",at:0)//插入元素 let mapleSyrup = shoppingList.remove(at:0) //移除元素
for (index,value) in shoppingList.enumerated() {//获取数组元素以及对应的索引
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() { //按照索引排序 和数组一样
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") { // 更换对应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") { // 移除对应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 返回一个数组
// airportCodes is ["YYZ", "LHR"]
let airportNames = [String](airports.values) // 获取字典所有的value 返回一个数组
// airportNames is ["Toronto Pearson", "London Heathrow"]