Dictionaries In Swift Programming Language

Introduction

As far as possible, I am trying to make a series of articles on Swift programming language. In this article, we will implement the concept of dictionaries in Swift.  in other languages, it is similar to Associative Array, Map and Hashable.

What is Dictionary?

  • A dictionary is an unordered collection of elements, where each element is compressed in key and a value.
    collection
  • In the image, given above, we have a unique key and a value. Remember that the same values appear on the dictionary but the same key can’t appear or can’t duplicate in the dictionary; it's always unique.

  • The best example of the dictionary is our book, that you can see in a chapter index page, that if we go to chapter eight, you skip the chapters, look up the page number and go to that chapter, which is shown in the index.

Creating Dictionaries

You can declare a dictionary by giving its key and its value as both of the the things are very important with its data type. Here is the declaration which enables you to understand your concept also.

let dictionaryexample: Dictionary <String, Int>

Swift can also give an option to declare a dictionary with the type on the initializer.

let initializeddictionary = Dictionary <String, Int> ()

Fortunately, Swift also gives a way to declare a dictionary in a short way.

let shortdictionary = [String: Int] ()

Accessing Values in Dictionaries

Likewise, in an array, we have several ways to access the the values of the dictionary. Dictionary supports subscripting to access the values. Here is the example of running the code in Xcode editor:

code

In this image, we first declare a dictionary of two key value pairs and then we print the complete dictionary, but if we want to print the specific value, you can only give its key but remember that the Optionals symbol is printed with that value. To avoid this symbol, use the exclamation sign. If we want to access those values which are not present in the dictionary, it returns nil.

Code

var dictionary = [1:"US",2:"PK"]
print(dictionary)

print(dictionary[1])
print(dictionary[1]!)
print(dictionary[3])

Dictionary Properties and Methods

Likewise, in an array, you have some built-in methods to check whether  that dictionary is empty or not. Here is the example which enables you to think about the dictionary methods:

code

In this image, we implement some built in methods like count, isEmpty() to check whether the dictionary is empty or not and it always returns true and false only. The count method checks how many keys are present in the dictionary.

Code

var dictionary = [1:"US",2:"PK"]
print(dictionary)

print(dictionary.count)
print(dictionary.isEmpty)

Modifying Dictionary

You can easily modify and update the value in the dictionary by using its built in method. You have two ways to update the value in the dictionary.

code

In this image, we declare a dictionary and call the updateValue method, while the other method gives the key and assigns the value. This is also a short way declaration.

Code

var dictionary = [1:"US",2:"PK"]
print(dictionary)

dictionary.updateValue("CA", forKey: 3)
print(dictionary)

dictionary[4] = "HX"
print(dictionary)

Removing Element from Dictionary

In Swift, it also gives open permission to delete the element from the dictionary by just calling its built in method and then gives the key of a value.

code

In this example,we call the method removeValueForKey, and it gives the key. The other way gives the keys and assigns it into nil. It also deletes the value from the dictionary and lastly, I will apply these two methods and our dictionary is now empty.

Code

var dictionary = [1:"US",2:"PK"]
print(dictionary)

dictionary.removeValueForKey(1)
print(dictionary)
dictionary[2]=nil
print(dictionary)

Conclusion

This is all about the topic dictionary in Swift. I am trying to cover each and every thing in this topic and if you have some issue in this topic , you can message me. I will solve your problem as soon as possible.


Similar Articles