A dictionary is a container that stores multiple values of the same type, that is, key-value pairs, just like json in js!
Swift's dictionary is defined using Dictionary <Key, Value>, where Key is the data type of the keys in the dictionary, and Value is the data type of the dictionary corresponding to the values stored by these keys.
Note: The key type of a dictionary must follow the Hashable protocol, just like the type of the Set value we mentioned earlier, it must also follow the Hashable protocol.
Create an empty dictionary
var namesOfIntegers = [Int: String] ()
namesOfIntegers [16] = "sixteen"
namesOfIntegers = [:]
Create a dictionary from a dictionary literal
var airports: [String: String] = ["XYZ": "Toronto Pearson", "DUB": "Dublin"]
// Because Swift can infer the type, it can also be equivalent to
var airports = ["XYZ": "Toronto Pearson", "DUB": "Dublin"]
Access and modify dictionary
twenty one
// Use count to get the number of elements in the dictionary
airports.count
// Use isEmpty to determine whether the number of dictionary elements is 0
if airports.isEmpty {
Zh
}
// Dictionary subscript syntax
airports ["LHR"] = "London" /// Reset new key-value pairs
airports ["LHR"] = "London Heathrow" /// Modify the value corresponding to a key
Zh
// Use updateValue (_: forKey :), the return value is the old value or nil corresponding to the current key, which means the return value is an optional value
if letlet OldValue = airports.updateValue ("Dublin Airport", forKey: "DUB") {
Print ("\ (oldValue)")
}
Zh
// The same subscript syntax to get the value corresponding to a key is also an optional value
if letlet airportName = airports ["DUB"] {
Print ("\ (airportName)")
} else {
Print ("nil")
}
Remove an element from the dictionary
airports ["APL"] = "Apple Internation"
// Remove the element corresponding to a key of the dictionary by assigning a value to nil
airports ["APL"] = nil
Zh
// Use removeValueForKey (_ :), return the old value or nil
if etlet removedValue = airports.removeValueForKey ("DUB") {
Print ("\ (removedValue)")
} else {
Print ("nil")
}
Dictionary traversal
for (airportCode, airportName) in airports {
Print ("\ (airportCode): \ (airportName)")
}
Zh
// Use the keys and values properties of the dictionary to get all the key and value values
for airportCode in airports.keys {
Print ("\ (airportCode)")
}
for airportName in airports.values {
Print ("\ (airportName)")
}
Create an array with dictionary keys and values
1
2
3
let airportsCode = [String] (airports.keys)
// keys with a certain sorting ability
let airportsSortCode = [String] (airports.keys.sort ())
————————————————
Copyright statement: This article is the original article of CSDN blogger "Long Road Along", and follows the CC 4.0 BY-SA copyright agreement.
Original link: https://blog.csdn.net/u013676544/article/details/51582998
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.