Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Dictionaries in Swift

DZone 's Guide to

Dictionaries in Swift

In this post, we introduce and delve into what exactly a dictionary is in Swift and how developers can use it.

· Web Dev Zone ·
Free Resource
Swift

Recently, 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 the 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 the dictionary is not maintained. If you are a Java developer, you can correlate this to a Hashtable which gives fast access to its entries.

Defining a Dictionary

A dictionary is 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 an immutable dictionary. To create a mutable dictionary, just use the 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 a string since we have used strings. Suppose we want to create an empty dictionary — we'd 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 three items in the dictionary.

Update a Value in a Dictionary

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

Method 1

var person = ["firstname":"John", "lastname":"Doe", "location":"Texas"] person["firstname"] = "Jane"

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

Method 2

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

The variableupdatedValue 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 the 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 them as follows:

person.removeAll()

Counting in a Dictionary

The size of the dictionary can be obtained by calling the 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 a dictionary in Swift. A detailed documentation can be found on the Apple Developer site.

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

Happy learning!

Topics:
web dev ,swift tutorial ,dictionary ,ios development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}