Add Elements to a Dictionary in Swift



In Swift, a dictionary is an unordered collection in which data is stored in the form of key-value pairs. To add elements to a dictionary we can use any of the following methods ?

  • Using Bracket notation

  • Using updateValue() method

Method 1: Using Brackets Notation

We can key-value pairs in the dictionary using bracket notation or we can say that using subscript notation. In this method, we can add a new key in the brackets and assign the corresponding value. If the specified key already exists, then it will replace the value of that key we the new value.

Syntax

dict[key] = "value"

Here, the key represents the key and value represent the associated value of the current key and the dict represents the dictionary.

Example

In the following Swift program, we will add elements to a dictionary. So create a dictionary with some key-value pairs. Then we use subscript notation through which we add three new key-value pairs in the given dictionary and display the final output.

import Foundation
import Glibc

// Creating a dictionary
var dict = ["apple": 3, "banana": 5, "orange": 2]

print("Dictionary(Before):", dict)

// Adding new key-value pairs
dict["mango"] = 8
dict["Kiwi"] = 19
dict["grapes"] = 10

// Displaying output
print("Dictionary(After):", dict)

Output

Dictionary(Before): ["apple": 3, "banana": 5, "orange": 2]
Dictionary(After): ["grapes": 10, "mango": 8, "banana": 5, "apple": 3, "orange": 2, "Kiwi": 19]

Method 2: Using updateValue(_:forKey) method

In Swift, using the updateValue() method we can update the value stored in the dictionary for the given key. Also, add new key-value pair if the specified key does not exist.

Syntax

dict.updateValue(nvalue, forKey: nkey)

Here, nvalue represents the new value and nkey represents the key to which we want to update or add value. If the given key exists in the dictionary, then its value is replaced by the new value. If the given key does not exist, then it will add the nkey and nvalue in the dictionary.

Example

In the following Swift program, we will add elements to a dictionary. We create a dictionary with some key-value pairs. Then use the updateValue() method to add new key-value pairs in the specified dictionary. Finally, display the output.

import Foundation
import Glibc

// Creating a dictionary
var dict = ["apple": 3, "banana": 5, "orange": 2]

print("Dictionary(Before):", dict)

// Adding new key-value pairs
dict.updateValue(8, forKey: "Peaches")
dict.updateValue(20, forKey: "graps")

// Displaying output
print("Dictionary(After):", dict)

Output

Dictionary(Before): ["banana": 5, "apple": 3, "orange": 2]
Dictionary(After): ["Peaches": 8, "orange": 2, "graps": 20, "banana": 5, "apple": 3]

Conclusion

So this is how we can add elements to a dictionary. Both methods do their job. Here both methods add one key-value pair at a time in the specified dictionary, you cannot assign multiple pairs at the same time.

Updated on: 2023-05-09T09:49:31+05:30

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements