本篇作为Swift的第二篇,主要简述Swift中的集合类型
[]let numbers: Array<Int>let inferredNumbers = Array<Int>() //类型推导let alsoInferredNumbers = [Int]() //简写,常用let evenNumber = [2, 4, 6, 8] //类型推到let allZero = [Int](count: 5, repeatedValue: 0) //[0, 0, 0, 0, 0]element[0]let numbers = numberArray[1...2]isEmpty countfirst last minElement() maxElement()contains(元素)append(元素) insert(元素) removeLast() removeAtIndex(索引)sort():返回排序后的数组的拷贝 sortInPlace():就地返回排序后的数组for-in 遍历数组元素reduce(_:combine:) filter(_:):按条件过滤元素到新数组 map(_:):对每个元素作指定操作然后映射到新数组let pairs: Dictionary<String, Int>let inferredPairs = Dictionary<Sting, Int>() //类型推导let alsoInferredNumbers = [String: Int]() //简写,常用let dict = ["A": 1, "B": 2, "C": 3][:]dict["A"] //Optional(1),不同与数组,如果没有对应键,则返回nil,不会运行时崩溃isEmpty countArray(dict.keys) Array(dict.values)dict.updateValue(值, forKey: 键)dict[键] = 值 //简写dict.removeValueForKey(键)`dict[键] = nil //简写for (key, value) in dicthashing:the process of transforming a value - String, Int, Double, Bool, etc - to a numeric value, known as the hash value.
let set: Set<Int>let set = Set<Int>() //类型推导[]let set: Set<Int> = [1, 2, 3, 1] //不声明类型就会认为是数组,这里1会被视为一个(唯一性)let set: Set<Int> = [1, 2, 3, 1] //类型推导isEmpty countcontains(值)first last 不知道返回哪一个(无序性),但是在集合只有0个或1个元素的时候会比较便捷insert(值)remove(值)for-inunion(_:) 创建一个新集合,该集合包含两个集合的并集intersect(_:) 创建一个新集合,该集合包含两个集合的交集subtract(_:) 创建一个新集合,该集合包含两个集合的补集subtract(_:) 创建一个新集合,该集合包含两个集合的并集减去交集