Updated April 11, 2023
Definition of Swift Dictionary
dictionary in Swift is used to store elements. Dictionary also contains one key while storing elements to it, later on, we can use these keys to access the elements store in the dictionary. Dictionary in swift does not maintain the insertion order of the element in which they are stored, they are unordered in nature. Also, they store the elements of the same type as value. In the coming section, we will discuss more their internal working in detail for better understanding. We can perform adding, deletion, filtering, modification, and many more operations using dictionary methods in swift which are very easy to sue and handle by the developer.
As discussed that dictionaries are used to store the elements, but the insertion order will not be maintained by them. Let’s see how we can create a dictionary in swift to use them while doing programming for better understanding see below;
var variable_name = [Key_Type: Value_Type]()
As you can see in the above lines of syntax we are creating a blank dictionary in swift. First, we have to give a name to the variable. Followed by the key type and variable type. We will discuss more these two things in detail in the coming section of our tutorial. For now, let’s see one practice syntax to get a better idea see below;
var mydemo = [String: String]()
The above lines of code have given you a better understanding of the dictionary in swift. In the next section, we will see how they work internally in swift by taking some examples.
How Dictionary works in Swift?
As now we already know that dictionary in swift is used to store the elements and perform some action when needed. But they store element of the same type this prevent us from any runtime error in the future. Also, they do not maintain the insertion order of the elements stored. while creating the dictionary object in swift we also have to pass the key for an element, by the use of it we can access the element later on. In this section, we will first discuss its working and later we will see one sample example for beginners to get the idea of how we can use this while programming in swift.
Internal working: Dictionary in swift takes two arguments while creating its object. These two arguments are ‘key’ and ‘value’. dictionary in swift works in the same like map work in java.
1) Key: In is used to access the value of the element stored in the dictionary. This has to be unique for each element present in the dictionary. Key can be an Integer and String. In terms of swift, these key is often referred to as ‘identifier’ of the element.
2) Value: This can be anything, also they can be duplicated because we access them by using the key we assign. While creating the value we give the type of the value as well, so they should be of same type throughout the programming for a particular object of dictionary in swift.
3) Fetch element from the dictionary: To access any element from the dictionary object we can use their key because they are unique. Below see the syntax to access them;
var variable_name = y_dictionary[key]
We can use ‘’ brackets to pass the key and get the result element as the output based on the key.
4) delete element from the dictionary: We can also delete element from the dictionary, for this, we have one method available in a dictionary named ‘removeValue’. Let’s see its syntax for better understanding see below;
Live Demo var variable_name:[Key:Value] =  var result_variable = variable_name.removeValue(forKey: your_key_here)
In the above lines of code, we are trying to delete elements from a dictionary based on the key. We are using ‘removeValue’ method from a dictionary. Inside this, we are passing our key to be deleted. After deletion, its value would assign to ‘nil’ in swift.
5) Modify the dictionary element: We can also do modification on the element for this we can use ‘updateValue(forKey:)’ method from dictionary. We can pass the new value of the element inside this method only. For better understanding see the syntax below;
var variable_name:[Key:Value] =  var resule_variable = someDict.updateValue("pass new value here , forKey: your_key)
In the above lines of code, we are trying to modify the value of a particular element using the key. Inside the ‘updateValue’ method we are passing two params as the input one is ‘new value’ and another one is ‘key’ of the element.
1) In this example we are trying to create the dictionary object after that we are assigning it various elements, Followed by assessing the elements using their ‘key’. This is a simple example for beginners to use while programming.
var mydemo:[String:String] = ["key 1":"value 1", "key 2":"value 2", "key 3":"value 3", "key 4":"value 4", "key 5":"value 5", "key 6":"value 6", ] print("Dictionary cerated successfully !!!") var result1 = mydemo["key 1"] var result2 = mydemo["key 2"] var result3 = mydemo["key 3"] var result4 = mydemo["key 4"] var result5 = mydemo["key 5"] print( "Value of first result is= 1 is \(result1)" ) print( "Value of second result is= 2 is \(result2)" ) print( "Value of third result is= three 1 is \(result3)" ) print( "Value of fourth result is= four is \(result4)" ) print( "Value of fifth result is= five is \(result5)" )
2) In this example we are trying to delete the value from the dictionary object for this we are using ‘removeValue’ method available in a dictionary object. Inside this method, we have pass the key that we want to delete and it will make its value as nil in result. This is a sample example for beginners.
var mydemo:[String:String] = ["key 1":"value 1", "key 2":"value 2", "key 3":"value 3", "key 4":"value 4", "key 5":"value 5", "key 6":"value 6", ] print("Dictionary cerated successfully !!!") var result1 = mydemo.removeValue(forKey: "key 1") var result2 = mydemo.removeValue(forKey: "key 2") var result3 = mydemo.removeValue(forKey: "key 3") var result4 = mydemo.removeValue(forKey: "key 4") var result5 = mydemo.removeValue(forKey: "key 5") print( "Value of first result is= 1 is \(result1)" ) print( "Value of second result is= 2 is \(result2)" ) print( "Value of third result is= three 1 is \(result3)" ) print( "Value of fourth result is= four is \(result4)" ) print( "Value of fifth result is= five is \(result5)" )
By using a dictionary we can store elements into it. To fetch them we have keys available inside them. Also, we can perform many operations like deletion, modify the element, filtering the element, accessing of the element, and so on. They provide us safety from any runtime error which can come up by casting of the element.
We hope that this EDUCBA information on “Swift Dictionary” was beneficial to you. You can view EDUCBA’s recommended articles for more information.