The Swift dictionary is used to store a collection of unordered data of the same type, and the Swift dictionary enforces the detection of the type of the element, which is an error if the type is different.
Each value of the Swift dictionary is associated with a unique key (key), which is used as the identifier for the value data in the dictionary.
The data items in the dictionary are not in a specific order, unlike the data items in the array. We use dictionaries when we need to access data through identifiers (keys), which is largely the same way that we use dictionaries in the real world to find meanings.
The key for the Swift dictionary does not have a type limit that can be integer or string, but must be unique.
If you create a dictionary and assign a value to a variable, the dictionary you create is modifiable. This means that after you create a dictionary, you can change the items in the dictionary by adding, deleting, and modifying them. If you assign a dictionary to a constant, the dictionary cannot be modified, and the size and contents of the dictionary cannot be modified.
Create
a dictionary//create a specific type of empty dictionary, in the format: var dict = [Keytype:valuetype] ()
//Create an empty dictionary, the type of the key is Int, the value is of a simple syntax of type String:
var di ct01 = [int:string] ()
print (dict01)
//Create an instance of a dictionary:
var dict02: [Int:string] = [1: "One", 2: "One", 3: "Three"]< C6/>print (dict02)
var dict03 = ["Name": "Devzhang", "Job": "Iosdev", "Company": "Vstecs"]
print (dict03)
Access Dictionary
//We can access the elements of an array based on the dictionary's index, with the following syntax: var value = Dict[key] Let
value01 = dict02[1]
print (VALUE01) lets
VALUE02 = dict03["name"]
print (VALUE02)
Add data let
value03 = Dict02.updatevalue ("Four", Forkey:4) //or dict02[4] = "Four"
print (VALUE03)
Print (dict02)
Modify dictionary
//Method 1 using Updatevalue (forkey:) Add or update the contents of the dictionary. If key does not exist, the value is added and if present, the value corresponding to the key is modified. The format is: the Dict.updatevalue (value, Forkey:key) method returns the optional value.
var value04 = dict02.updatevalue ("twotmp", Forkey:2)
print (dict02)
print (VALUE04)
//Method 2 through the specified Key to modify the value of the dictionary
var value05 = dict02[3]
print (value05)
value05 = "threetmp"//Modify Invalid
print (dict02)
dict02[3] = "threetmp"//Modify valid
print (dict02)
Remove Key-value pair
//1 Use the Removevalueforkey () method to remove the dictionary key-value pair. If the key exists, the method returns the removed value if no return nil exists. Let
valueRemove01 = Dict02.removevalueforkey (2)
print (VALUEREMOVE01)
print (dict02)
//2 by specifying the value of the key to Nil to remove the Key-value (key-value) pair.
dict02[1] = nil
print (dict02)
Traverse dictionary
//1 Use the for-in loop to traverse a key-value pair in a dictionary.
for (key, value) in dict03
{
print ("Dictionary key \ (key)- dictionary value \ (value)")
}
//2 uses the enumerate () method to iterate through the dictionary, returning the index of the dictionary and (key, value) to
for (key, value) in Dict03.enumerate ()
{
print ("Dictionary key \ (key)- dictionary (key, value) to \ (value)")
}
//3 for
key in Dict03.keys
{
Let value = Dict03[key]
print ("key = \ (key), value = \ (value)")
}
The dictionary converts the
key value (key-value) pairs of the array//extraction dictionary and converts it to a separate array. Let
dictkeys = [String] (Dict03.keys) for
(key) in Dictkeys
{
print ("\ (key)")
}
Let Dictvalues = [String] (dict03.values) for
(value) in Dictvalues
{
print ("\ (value)
}}
Count Property Let
count01 = dict03.count
print (count01)
IsEmpty Property Let
empty01 = dict01.isempty
print ("dict01 are \ (empty01)") let
empty02 = Dict03.isempty
Print ("dict03 is \ (empty02)")