Swift Collections : Dictionaries

Today we are going to talk about dictionaries. Dictionaries are one of the collections used in Swift. They are not as commonly used as arrays but the functionality that dictionaries have make them powerful. A dictionary is a container that stores data as key-value pairs. In dictionaries all the keys must be the same type as well as all the values must be the same type. Key is used as the index of a certain value in the dictionary. So that means all the keys must be unique. Dictionaries also store data in an unordered format. We must be aware of it while using a dictionary. It is very useful in abbreviations. For example we can use a dictionary for country abbreviation examples. Below table shows an example of a few countries with it’s abbreviations.

UKUnited Kingdom

Let’s create a dictionary according to the preceding table.

let countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]

In the preceding code we define countries named dictionary as immutable. To create a mutable dictionary just replace the let keyword with the var as shown below :

var countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]

In the preceding example both the key and the value pair are defined as the string type by the compiler because we instantiated the dictionary with default value. If we want to create an empty dictionary we must tell the compiler which types will be used by key and value The following code shows how to create an empty dictionary :

var dictionary1 = [String:String]()  
var dictionary2 = [Int:String]()
var dictionary3 = [String:MyObject]()  
var dictionary4: [String:String] = [:]  
var dictionary5: [Int:String] = [:]

You can also define a custom type into a dictionary as shown above code in dictionary3. The first type defines the key type of the dictionary while the second type represents it’s value type. 

As we said before, we use the key to access it’s value. If the dictionary does not contain the key that we are looking for it will return nil value. Therefore the variable that returned from that lookup is optional. Below code shows how to access a value of “TR” key :

var countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]
var name = countries["TR"]

When we run the above code, the name variable will have Turkey value . We can use the count property to get the count of key-value pairs in the dictionary.

var countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]
var cnt = countries.count

The cnt variable will contain 3 since the countries dictionary has three key-value pairs.To check whether a dictionary is empty we can use isEmpty property. It will return false if there is one or more key-value pairs in the dictionary otherwise the return value will be true.

var countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]
var empty = countries.isEmpty

To update a value in a dictionary there are two different approaches. We can use subscript syntax which most of the programmers are familiar with. We can also use updateValue(_:, forKey:)  method. This method also returns the old value that we attempt to change in the dictionary. If the key that we send to updateValue(_:, forKey:) method does not exist in the dictionary it will add the value with the key as a new key-value pair. We can also use subscript syntax to add new value. Below codes show how to add a new key-pair and how to update a value :

var countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]
countries["EG"] = "Egypt"  // add a new key-pair
countries["TR"] = "New Turkey" // update TR key's value
var oldValue = countries.updateValue("New Indonesia", forKey: "ID") // update ID key's value

Sometimes you may need to remove one or more key-value pairs from a dictionary. To do that there are three approaches. We can use subscript syntax as defining the nil value to a key which we want to remove. Other than this, we can use removeValue(forKey:) method. This method returns the removed value as a variable. You may want to notify the user about the removed value If you want to remove all the key-value pairs from a dictionary you can use removeAll() method. Below codes show how to use these three approaches to remove elements from a dictionary.

var countries = ["TR":"Turkey","UK":"UnitedKingdom","ID":"Indonesia"]
countries["UK"] = nil
var removedValue = countries.removeValue(forKey:"ID") // removedValue will have "Indonesia"
countries.removeAll() // it will remove all the key-value pairs from the dictionary

I think that’s pretty enough for explaining the dictionaries in swift. I will see you in the next post. Peace …

You may also like...