Swift 笔记(十)
我的主力博客:半亩方塘 Dictionaries 1、A dictionary is an unordered collection of pairs,where each pair is comprised ofa keyanda value. The same key can’t appear twice in a dictionary,but different keys may point to the same value.All keys have to be of the same type,and all values have to be of the same type. 2、When you create a dictionary,you can explicitly declare the type of the keys and the type of the values:
let pairs: Dictionary<String,Int> Swift can also infer the type of a dictionary from the type of the initializer:
let inferredPairs = Dictionary<String,Int>() Or written with the preferred shorthand:
let alsoInferredPairs = [String: Int]() Within the square brackets,the type of the keys is followed bya colonand the type of the values. This is the most common way to declare a dictionary. If you want to declare a dictionary with initial values,you can use a dictionary literal. This is a list of key-value pairs separated by commas,enclosed in square brackets: let namesAndScores = ["Anna": 2,"Brian": 2,"Craig": 8,"Donna": 6] print(namesAndScores) // > ["Brian": 2,"Anna": 2,"Donna": 6] In this example,the dictionary has pairs of An empty dictionary literal looks like this: var emptyDictionary: [Int: Int] emptyDictionary = [:]
print(namesAndScores["Anna"]) // > Optional(2) Notice that the return type is an optional. The dictionary will check if there’s a pair with the key “Anna”,and if there is,return its value. If the dictionary doesn’t find the key,it will return nil:
print(namesAndScores["Greg"]) // > nil Properties and methods:
print(namesAndScores.isEmpty) // > false print(namesAndScores.count) // > 4 print(Array(namesAndScores.keys)) // > ["Brian","Anna","Craig","Donna"] print(Array(namesAndScores.values)) // > [2,2,8,6] 4、Take a look at Bob’s info:
var bobData = ["name": "Bob","profession": "Card Player","country": "USA"] Let’s say you got more information about Bob and you wanted to add it to the dictionary. This is how you’d do it:
bobData.updateValue("CA",forKey: "state") There’s even a shorter way,using subscripting: bobData["city"] = "San Francisco" You want to change Bob’s name from Bob to Bobby: bobData.updateValue("Bobby",forKey: "name"] // > Bob Why does it return the string “Bob”? As with adding,you can change his profession with less code by using subscripting:
bobData["profession"] = "Mailman" Like the first method,the code updates the value for this key or,if the key doesn’t exist,creates a new pair. You want to remove all information about his whereabouts: bobData.removeValueForKey("state") As you might expect,there’s a shorter way to do this using subscripting:
bobData["city"] = nil Assigning 5、The for (key,value) in namesAndScores { print("(key) - (value)") } // > Brian - 2 // > Anna - 2 // > Craig - 8 // > Donna - 6 It’s also possible to iterate over just the keys: for key in namesAndScores.keys { print("(key),",terminator: "") // no newline } print("") // print one final newline // > Brian,Anna,Craig,Donna, You can iterate over just the values in the same manner with the 6、You could use
let namesString = namesAndScores.reduce("",combine: { $0 + "($1.0)," }) print(namesString) In a reduce statement, Let’s see how you could use print(namesAndScores.filter({ $0.1 < 5 })) // > [("Brian",2),("Anna",2)] Here you use (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |