Dictionaries in Swift

In the last days, I have been learning and coding in Swift. Swift is a powerful and general-purpose language. As a seasoned Java developer, I can correlate some of the Swift’s concepts and it is easy to learn.

In this article, I would like to introduce and delve into dictionary in Swift. Dictionary is a container which stores key-value pairs. All the keys in the dictionary are of the same type and all the values are of the same type. The order in dictionary is not maintained. If you are a Java developer, you can correlate this to Hashtable which gives fast access to its entries.

Defining a Dictionary

Dictionary is nothing but a comma separated key-value pair and defining such a dictionary literal is simple. Let us see the following example:

let person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

Here, notice that I have used the keyword let which creates immutable dictionary. To create a mutable dictionary, just use var keyword.

var person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

In the above examples, the compiler automatically identifies the type of keys and values. In this case, it is string since we have used strings. Suppose, if we want to create an empty dictionary, we need to tell the compiler about types as follows:

var sample1 = [String:String]()
var sample2 = [Int:String]()
var sample2 = [Int:CustomObject]()

Reading Dictionary Values

If we want to read a value based on its key, we need to use subscript syntax.

let person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

var value = person[“firstname”]

This should output the string “John”.

Checking If Dictionary Empty

We can check whether the dictionary is empty by using the following syntax.

let person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

var value = person.IsEmpty

This returns false as it contains 3 items in the dictionary.

Update a Value in Dictionary

The values in the dictionary can be updated using their keys in two methods.

Method 1

var person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

person[“firstname”] = “Jane”

This update the value of the first key-value pair.

Method 2

var updatedValue = person.updateValue[“Jane”, forKey: “firstname”]

The variable updatedValue would contain the string “Jane”.

Adding and Removing a key-value pair

We can add or remove a key-value pair easily to a dictionary. We can use either subscript syntax or the updateValue(_:, forKey:) method.

Add

var person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

person[“occupation”] = “Technician”

This just adds a new entry to the above dictionary.

var updatedDictionary = person.updateValue(“Technician”, forKey: “occupation”]

Here, the variable updatedDictionary would be set to nil because there is no value associated with key “occupation”.

Remove

var person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

person[“location”] = nil

This will remove the location key-value pair.

We can also remove the key-value pair using removeValue(forKey:).

var updatedDictionary = person.removeValue(forKey:”location”)

The location key-value pair would be removed. The variable updatedDictionary would contain the value “Texas”.

If we would like to remove all the entries in dictionary, we can simply remove as follows:

person.removeAll()

Getting Count in Dictionary

The size of the dictionary can be obtained by calling count property.

var person = [“firstname”:”John”, “lastname”:”Doe”, “location”:”Texas”]

var count = person.count

This gives the count 3.

Conclusion

We just covered the basics of Dictionary. A detailed documentation can be found on Apple Developer site.

I hope you enjoyed my article. Feel free to leave any comments or suggestions.

Happy learning!

Advertisements