Swift 5 Dictionary usage Daquan

System: Mac OS 10.15.2, XCode 11.3, swift 5.0
Writing Time: 2020-01-08

Explanation

Dictionary a dictionary key-value pair, the container is more commonly used, the initialization format.

var someDict = [KeyType: ValueType]()

Creating Dictionary

It can create an empty container, or initialized to the value specified

var dict = [Int: String]()
var dict1:[Int: String] = [1:"One", 2:"Two", 3:"Three"]

Sequence Based Initialization

By key array, Value array, create the dictionary.

var cities = ["Delhi", "Bangalre", "Hyderabad"]
var distance = [2000, 10, 620]
let cityDistanceDict = Dictionary(uniqueKeysWithValues: zip(cities, distance))

Filtering

By filtering out a child does not meet the criteria.

var closeCities = cityDistanceDict.filter{ $0.value < 1000 }
print("\(closeCities)")
// closeCities > ["Bangalore": 10, "Hyderabad": 620]

Dictionary Grouping

To the dictionary group, the following for the first character of the same sub-items are divided into the same group.

var city = ["Delhi", "Bangalore", "Hyderabad", "Dehradun", "Bihar"]
var GroupedCities = Dictionary(grouping: city) { $0.first! }
print("\(GroupedCities)")
// ["D" :["Delhi","Dehradun"], "B" : ["Bengaluru","Bihar"], "H" : ["Hyderabad"]]

Accessing Dictionaries

The value obtained by the orientation of the dictionary key value index
var someVar = someDict[key]

var someDict:[Int: String] = [1:"One", 2:"Two", 3:"Three"]
var someVar = someDict[1]

print("Value of key = 1 is \(someVar)")
print("Value of key = 2 is \(someDict[2])")
print("Value of key = 3 is \(someDict[3])")
//Value of key = 1 is Optional("One")
//Value of key = 2 is Optional("Two")
//Value of key = 3 is Optional("Three")

Modifying Dictionaries

Value can be modified by the method updateValue, by way of assignment may be.

var oldVal = someDict.updateValue("New value of one", forKey: 1)
someVar = someDict[1]
someDict[2] = "New value of two"

print("Old value of key = 1 is \(oldVal)")
print( "Value of key = 1 is \(someVar)" )
print( "Value of key = 2 is \(someDict[2])" )
print( "Value of key = 3 is \(someDict[3])" )

//Old value of key = 1 is Optional("One")
//Value of key = 1 is Optional("New value of one")
//Value of key = 2 is Optional("New value of two")
//Value of key = 3 is Optional("Three")

Remove Key-Value Pairs

Delete key-value pairs, by the method removeValue, or by setting a value of nil.

someDict = [1:"One", 2:"Two", 3:"Three"]
var removeValue = someDict.removeValue(forKey: 2)
someDict[3] = nil
print( "Value of key = 1 is \(someDict[1])" )
print( "Value of key = 2 is \(someDict[2])" )
print( "Value of key = 3 is \(someDict[3])" )
//Value of key = 1 is Optional("One")
//Value of key = 2 is nil
//Value of key = 3 is nil

Iterating Over a Dictionary

The right way to traverse the dictionary as follows:

someDict = [1:"One", 2:"Two", 3:"Three"]

for (key, value) in someDict {
    print("Dictionary key \(key) - Dictionary value \(value)")
}
//Dictionary key 3 - Dictionary value Three
//Dictionary key 1 - Dictionary value One
//Dictionary key 2 - Dictionary value Two

Note: If the traversal someDict.enumerated(), actually get the key from the zero-based index, value is keyValue child (key: 3, value: "Three").

for (index, keyValue) in someDict.enumerated() {
    print("index \(index) - Dictionary value \(keyValue)")
}
//index 0 - Dictionary value (key: 3, value: "Three")
//index 1 - Dictionary value (key: 1, value: "One")
//index 2 - Dictionary value (key: 2, value: "Two")

Convert to Arrays

Dictionary of keys and values ​​are arrays, can be converted into a single array with the Note is converted to an array, the array is not Set is not repeated, can observe the two value are Two, in fact, two Two are printed out.

someDict = [1:"One", 2:"Two", 3:"Two"]
let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)

print("Print Dictionary Keys")
for (key) in dictKeys {
    print("\(key)")
}

print("Print Dictionary Values")
for (value) in dictValues {
    print("\(value)")
}
//Print Dictionary Keys
//2
//3
//1
//Print Dictionary Values
//Two
//Two
//One

The count Property

The number of dictionary to get through the count property.

var someDict1: [Int: String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2: [Int: String] = [4:"Four", 5:"Five"]
print("Total items in someDict1 = \(someDict1.count)")
print("Total items in someDict2 = \(someDict2.count)")
//Total items in someDict1 = 3
//Total items in someDict2 = 2

The empty Property

Dictionary determines whether a controlled, properties can be determined by isEmpty

var someDict3:[Int:String] = [Int:String]()
print("someDict1 = \(someDict1.isEmpty)")
print("someDict2 = \(someDict2.isEmpty)")
print("someDict3 = \(someDict3.isEmpty)")
//someDict1 = false
//someDict2 = false
//someDict3 = true

reference

https://www.tutorialspoint.com/swift/swift_dictionaries.htm

发布了127 篇原创文章 · 获赞 12 · 访问量 2万+

Guess you like

Origin blog.csdn.net/zgpeace/article/details/103888806