swift 4.2 官方文档学习笔记(四) |
您所在的位置:网站首页 › sorted返回值 › swift 4.2 官方文档学习笔记(四) |
swift 4.2 官方文档学习笔记(四)- Array Set 和 Dictionary 这一章来讲一下各种collection,集中在集合的创建,添加和删除等功能 先来看看定义: Array:有序value的集合Set:无序Value的集合Dictionary:无序 key-value pair的集合关于三种不同类型的collection,他们的类型在声明时就确定了,在使用时非常确定是什么数据类型的(也可以是泛型): Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you cannot insert a value of the wrong type into a collection by mistake. 1. mutable or immutable? Array:2. 创建Array: var someInts = [Int]() //创建空数组 var threeDoubles = Array(repeating: 0.0, count: 3) var sixDoubles = threeDoubles + anotherThreeDoubles //用两个数组的和来创建新数组 var shoppingList: [String] = [“Eggs”, “Milk”] //用real value来创建数组 3.使用 enumerated() 遍历数组,可以同时返回一个包含索引和值的元组: for (index, value) in shoppingList.enumerated() { print(“Item \(index + 1): \(value)”) } // Item 1: Six eggs // Item 2: Milk // Item 3: Flour // Item 4: Baking Powder // Item 5: Bananas 4.Hash Values for Set Types:所有储存在Set中的数据必须是hashable的,我们常见的数据类型:string,Int,Bool,Double等都是hashable数据,所以在存储时可以放心存储,关于枚举要注意,普通的枚举值也是hashable的,但是有关联值的枚举成员值(后面有个括号包含了一些数据类型的那种)就不是hashable的了。当 a = b时,这两个数的哈希值应该也是相同的 Set5. 创建Set的几种方法 let letters = Set() var favoriteGenres: Set = [“Rock”, “Classical”, “Hip hop”] 或者简化为(set不能省略):var favoriteGenres: Set = [“Rock”, “Classical”, “Hip hop”] 6. set 常用的函数:set.insert(“a”) / favoriteGenres.remove(“Rock”) /favoriteGenres.isEmpty / favoriteGenres.count / favoriteGenres.contains(“Funk”) 7. 遍历Set : for genre in favoriteGenres for genre in favoriteGenres.sorted() 8. set 还有一个很有趣的地方,是关于两个合集的运算,两个合集可以: 使用 intersection(_:)方法来创建一个只包含两个合集共有值的新合集; 使用 symmetricDifference(_:)方法来创建一个只包含两个合集各自有的非共有值的新合集; 使用 union(_:)方法来创建一个包含两个合集所有值的新合集; 使用 subtracting(_:)方法来创建一个两个合集当中不包含某个合集值的新合集。 let oddDigits: Set = [1, 3, 5, 7, 9] let evenDigits: Set = [0, 2, 4, 6, 8] let singleDigitPrimeNumbers: Set = [2, 3, 5, 7] oddDigits.union(evenDigits).sorted() // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] oddDigits.intersection(evenDigits).sorted() // [] oddDigits.subtracting(singleDigitPrimeNumbers).sorted() // [1, 9] oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted() // [1, 2, 9] 9, 还有判断两个合计的关系,子集 or 超集 or 完全具有不同的值 使用“相等”运算符 ( ==)来判断两个合集是否包含有相同的值; 使用 isSubset(of:)方法来确定一个合集的所有值是被某合集包含; 使用 isSuperset(of:)方法来确定一个合集是否包含某个合集的所有值; 使用 isStrictSubset(of:)或者 isStrictSuperset(of:)方法来确定是个合集是否为某一个合集的子集或者超集,但并不相等; 使用 isDisjoint(with:)方法来判断两个合集是否拥有完全不同的值。 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:1. 创建dictionary: var namesOfIntegers = [Int: String]() //namesOfIntegers = [:] var airports: [String: String] = [“YYZ”: “Toronto Pearson”, “DUB”: “Dublin”] var airports = [“YYZ”: “Toronto Pearson”, “DUB”: “Dublin”] //简化 2. 常用到的方程包括:airports.count / airports.isEmpty / airports[“LHR”] = “London” / oldValue = airports.updateValue(“Dublin Airport”, forKey: “DUB”) 可以返回值表示更新是否成功,在设定值之前key可以不存在 // removedValue = airports.removeValue(forKey: “DUB”) 有值返回值,没有值返回nil 3. 遍历 for (airportCode, airportName) in airports for airportCode in airports.keys for airportName in airports.values let airportCodes = [String](airports.keys) //将所有key形成一个数组返回 let airportNames = [String](airports.values) // 将所有value形成一个数组返回 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |