Swift Dictionaries

In this tutorial you will learn about the Swift Dictionaries and its application with practical example.

Swift Dictionaries

In Swift Dictionary is an unordered collection of multiple key-value pairs of same type. Dictionary is similar to associative arrays or hashes consist of key-value pairs. In dictionary each of the value is associated with a unique key, and this key is used to accessed corresponding dictionary value. In Swift, dictionaries are strongly typed, which means it is mandatory to specify types for both the keys and the values.

All the keys in a dictionary have the same type and all the values also have the same type, it is not necessary that the keys and values both of the same type. The type of any dictionary can be determined by the type of the keys and the type of the values it holds. A dictionary of type[Int:String] has keys of type Int and values of type String.

Declaring Dictionaries

In Swift, a dictionary can be declared using square brackets as following –

Syntax 1:-

OR

Syntax 2:-

Here, DictionaryName is replaced with the actual dictionary variable name and KeyType and ValueType are replaced with the data type of key and value respectively.

Initializing Dictionary

In Swift, we can initialize a dictionary with a dictionary literal as following –

Syntax:-

Here, a dictionary literal is a list of key-value pairs, separated by commas, surrounded by a pair of square brackets. A key-value pair is a combination of a key and a value separate by a colon(:).

Example:-

Swift Initializing an Empty Dictionary

There are multiple ways we can initialize an empty dictionary –

Syntax 1:- Full Syntax

Syntax 2:- Shorthand Method

Syntax 3:- Empty Dictionary Using Dictionary Literals

Note:- If we don’t specify the type explicitly then Swift automatically infers the type.

Accessing Dictionary Elements In Swift

In swift a dictionary element can simply be accessed using subscript syntax, in order to access any specific dictionary element we need to pass associated key within the brackets.

Example:-

Output:-

swift_accessing_dictionary_elements

Here, dictionary value returned by subscript can be unwrapped using optional binding or forced value operator (!) as following –

Example:-

Swift Checking If a Dictionary Is Empty (isEmpty)

In Swift, isEmpty property can be used to check whether the given dictionary is empty or not.The isEmpty property returns a boolean value to indicate whether the dictionary is empty or not.

Example:-

Output:-

swift_check_dictionary_isempty

Swift Get Dictionary Elements Count

In swift count property can be used to get the count of elements of given dictionary.

Example:-

Output:-

swift_dictionary_count

Swift Add Elements to Dictionary

In swift, we can add a new item to dictionary by simply taking a new key of appropriate type and assign new value of appropriate type to it.

Example:-

Output:-

swift_add_item_dictionary

Swift Update Dictionary Element

The simplest way update a dictionary element values by assigning new values to associated key.

Example:-

Output:-

swift_dictionary_update

Dictionary element can also be updated using updateValue(_, forKey: _) method as following –

Example:-

Output:-

swift_dictionary_updatevalue

Removing Dictionary Elements in Swift

The simplest way remove a dictionary element values by assigning nil to associated key, alternatively we can remove an element using removeValue(forKey: _) method.

Example:-

Output:-

swift_dictionary_remove_element

Iterating Over a Dictionary in Swift

In Swift, we can loop through the dictionary elements using for-in loop as following –

Example:-

Output:-

swift_iterating_dictionary_elements

Iterating Over a Dictionary Keys

In Swift, we can loop through the dictionary keys only using keys property as following –

Example:-

Output:-

swift_iterating_dictionary_keys

Iterating Over a Dictionary Values

In Swift, we can loop through the dictionary values only using values property as following –

Example:-

Output:-

swift_iterating_dictionary_values

 

In this tutorial we have learn about the Swift Dictionaries and its application with practical example. I hope you will like this tutorial.