Swift dictionary
Swift dictionaries are used to store unordered collections of data of the same type. Swift dictionaries will forcefully detect the type of elements and report an error if the types are different.
Each value in the Swift dictionary is associated with a unique key, and the key serves as the identifier of the value data in the dictionary.
Unlike the data items in the array, the data items in the dictionary have no specific order. We use dictionaries when we need to access data through identifiers (keys). This method is largely the same as the way we use dictionaries to look up word meanings in the real world.
Swift dictionary keys can be integers or strings without type restrictions, but they must be unique.
If you create a dictionary and assign it to a variable, the created dictionary can be modified. This means that after the dictionary is created, the items in the dictionary can be changed by adding, deleting, or modifying. If you assign a dictionary to a constant, the dictionary cannot be modified, and neither the size nor the contents of the dictionary can be modified.
Create Dictionary
We can use the following syntax to create an empty dictionary of a specific type:
var someDict = [KeyType: ValueType]()
The following is to create an empty dictionary with the key type Int , the simple syntax of the value type is String:
var someDict = [Int: String]()
The following is an example of creating a dictionary:
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
Accessing the dictionary
We can based on the index of the dictionary To access the elements of the array, the syntax is as follows:
var someVar = someDict[key]
We can learn how to create, initialize, and access the dictionary through the following examples:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var someVar = someDict[1] print( "key = 1 的值为 \(someVar)" ) print( "key = 2 的值为 \(someDict[2])" ) print( "key = 3 的值为 \(someDict[3])" )
The output result of the above program execution is:
key = 1 的值为 Optional("One") key = 2 的值为 Optional("Two") key = 3 的值为 Optional("Three")
Modify dictionary
We can use updateValue(forKey:) to add or update the contents of the dictionary. If key does not exist, add the value. If it exists, modify the value corresponding to key. The updateValue(_:forKey:) method returns the Optional value. The example is as follows:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var oldVal = someDict.updateValue("One 新的值", forKey: 1) var someVar = someDict[1] print( "key = 1 旧的值 \(oldVal)" ) print( "key = 1 的值为 \(someVar)" ) print( "key = 2 的值为 \(someDict[2])" ) print( "key = 3 的值为 \(someDict[3])" )
The execution output of the above program is:
key = 1 旧的值 Optional("One") key = 1 的值为 Optional("One 新的值") key = 2 的值为 Optional("Two") key = 3 的值为 Optional("Three")
You can also modify the value of the dictionary by specifying the key, as shown below:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var oldVal = someDict[1] someDict[1] = "One 新的值" var someVar = someDict[1] print( "key = 1 旧的值 \(oldVal)" ) print( "key = 1 的值为 \(someVar)" ) print( "key = 2 的值为 \(someDict[2])" ) print( "key = 3 的值为 \(someDict[3])" )
The above program The execution output is:
key = 1 旧的值 Optional("One") key = 1 的值为 Optional("One 新的值") key = 2 的值为 Optional("Two") key = 3 的值为 Optional("Three")
Remove Key-Value pairs
We can use the removeValueForKey() method to remove dictionary key-value pairs. This method returns the removed value if key exists, or nil if it does not exist. An example is as follows:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var removedValue = someDict.removeValueForKey(2) print( "key = 1 的值为 \(someDict[1])" ) print( "key = 2 的值为 \(someDict[2])" ) print( "key = 3 的值为 \(someDict[3])" )
The above program execution output is:
key = 1 的值为 Optional("One") key = 2 的值为 nil key = 3 的值为 Optional("Three")
You can also remove key-value (key-value) pairs by specifying the value of the key as nil. The example is as follows:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] someDict[2] = nil print( "key = 1 的值为 \(someDict[1])" ) print( "key = 2 的值为 \(someDict[2])" ) print( "key = 3 的值为 \(someDict[3])" )
The execution output of the above program is:
key = 1 的值为 Optional("One") key = 2 的值为 nil key = 3 的值为 Optional("Three")
Traversing the dictionary
We can use the for-in loop to traverse Key-value pairs in a dictionary. The example is as follows:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] for (key, value) in someDict { print("字典 key \(key) - 字典 value \(value)") }
The execution output of the above program is:
字典 key 2 - 字典 value Two 字典 key 3 - 字典 value Three 字典 key 1 - 字典 value One
We can also use the enumerate() method to traverse the dictionary, which returns the dictionary index and (key, value) pair , the example is as follows:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] for (key, value) in someDict.enumerate() { print("字典 key \(key) - 字典 (key, value) 对 \(value)") }
The execution output of the above program is:
字典 key 0 - 字典 (key, value) 对 (2, "Two") 字典 key 1 - 字典 (key, value) 对 (3, "Three") 字典 key 2 - 字典 (key, value) 对 (1, "One")
Convert the dictionary to an array
You can extract the key-value of the dictionary Yes, and converted to a separate array. The example is as follows:
import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] let dictKeys = [Int](someDict.keys) let dictValues = [String](someDict.values) print("输出字典的键(key)") for (key) in dictKeys { print("\(key)") } print("输出字典的值(value)") for (value) in dictValues { print("\(value)") }
The execution output of the above program is:
输出字典的键(key) 2 3 1 输出字典的值(value) Two Three One
count attribute
We can use the read-only count attribute to calculate how many key-value pairs the dictionary has:
import Cocoa var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var someDict2:[Int:String] = [4:"Four", 5:"Five"] print("someDict1 含有 \(someDict1.count) 个键值对") print("someDict2 含有 \(someDict2.count) 个键值对")
The above program execution output The result is:
someDict1 含有 3 个键值对 someDict2 含有 2 个键值对
isEmpty attribute
Y We can use the read-only attribute isEmpty to determine whether the dictionary is empty and return a Boolean value:
import Cocoa var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var someDict2:[Int:String] = [4:"Four", 5:"Five"] var someDict3:[Int:String] = [Int:String]() print("someDict1 = \(someDict1.isEmpty)") print("someDict2 = \(someDict2.isEmpty)") print("someDict3 = \(someDict3.isEmpty)")
The execution output of the above program is:
someDict1 = false someDict2 = false someDict3 = true