| by Arround The Web | No comments

Accessing Elements from a Dictionary in Swift

In this Swift guide, we will see different scenarios for accessing elements from a Swift Dictionary.

Scenario 1- Using for loop

In this scenario, it can be possible to access key-value pairs individually from a swift dictionary using a for loop. This loop iterates the dictionary and returns key-value pairs individually.

Syntax:

for (key_iterator,value_iterator) in swift_dictionary {  
    print("Key - \(key_iterator) Value - \(value_iterator)")  
}

 
Here, key_iterator is used to iterate the keys and value_iterator is used to iterate the values.
Swift_dictionary is the dictionary name.

Example 1:

We will create a swift dictionary that has 5 key-value pairs and return key-value pairs individually using a for loop.

//create a swift Dictionary
let swift_dictionary = [1:"swift1",2:"swift2",3:"swift3",4:"swift4",5:"swift5"]  

print("Actual Dictionary - \(swift_dictionary)")

//iterate the swift_dictionary using for loop and
//get the keys and values one by one.
for (key_iterator,value_iterator) in swift_dictionary {  
    print("Key - \(key_iterator) Value - \(value_iterator)")  
}

 
Output:

Explanation:

Line 2-


We created a dictionary named swift_dictionary with 5 key-value pairs.

Line 5-


We displayed the entire dictionary.

Line 9-11


Using a for loop we returned key-value pairs.

Example 2:

We will create a swift dictionary that has 3 key-value pairs and return key-value pairs one by one using a for loop.

//create a swift Dictionary
let swift_dictionary = ["swift1":1,"swift2":2,"swift3":3]  

print("Actual Dictionary - \(swift_dictionary)")

//iterate the swift_dictionary using for loop and
//get the keys and values one by one.
for (key_iterator,value_iterator) in swift_dictionary {  
    print("Key - \(key_iterator) Value - \(value_iterator)")  
}

 
Output:

Explanation:

Line 2-


We created a dictionary named swift_dictionary with 3 key-value pairs. Key is String type and value is Integer type.

Line 5-


We displayed the entire dictionary.

Line 9-11


Using a for loop we returned key-value pairs.

Scenario 2- Accessing Values using Keys

In this scenario, we will use a particular Key to access the associated value. To do this, we need to use a square bracket and specify the key inside it.

Finally, we are unwrapping the result by specifying the “!” symbol.

Syntax:

swift_dictionary[Key]

 
Where swift_dictionary is the dictionary and Key represents the key value.

Example 1:

Here, we will create a swift dictionary such that Key is of String type and access the values using the Key.

//create a swift Dictionary
let swift_dictionary = ["swift1":1,"swift2":2,"swift3":3]  

print("Actual Dictionary - \(swift_dictionary)")

//get the value for key-"swift1"
print(swift_dictionary["swift1"]!)

//get the value for key-"swift2"
print(swift_dictionary["swift2"]!)

//get the value for key-"swift3"
print(swift_dictionary["swift3"]!)

 
Output:

Explanation:

The swift dictionary has 3 Key-value pairs.


Now, we are accessing all values using Keys.

Example 2:

Here, we will create a swift dictionary such that Key is of Integer type and access the values using the Key.

//create a swift Dictionary
let swift_dictionary = [1:"swift1",2:"swift2",3:"swift3"]  

print("Actual Dictionary - \(swift_dictionary)")

//get the value for key-1
print(swift_dictionary[1]!)

//get the value for key-2
print(swift_dictionary[2]!)

//get the value for key-3
print(swift_dictionary[3]!)

 
Output:

Explanation:

The swift dictionary has 3 Key-value pairs.


Now, we are accessing all values using Keys.

Scenario 3 – Accessing only Keys

Swift Dictionary supports keys property to return only Keys from the dictionary. This property will return all the keys from a swift dictionary into an array.

Syntax:

swift_dictionary.keys

 
Where: swift_dictionary is the input swift dictionary.

Example:

Let’s create a swift dictionary and return only keys.

//create a swift Dictionary
let swift_dictionary = [1:"swift1",2:"swift2",3:"swift3"]  

print("Actual Dictionary - \(swift_dictionary)")

//get only keys
print("Keys in a Dictionary - \(swift_dictionary.keys)")

 
Output:

Explanation:

We created a swift dictionary that has three key value pairs.


Finally using the keys property, we are returning keys.

Scenario 4 – Accessing only Values

Swift Dictionary supports values property to return only Values from the dictionary. This property will return all the values from a swift dictionary into an array.

Syntax:

swift_dictionary.values

 
Where, swift_dictionary is the input swift dictionary.

Example:

Let’s create a swift dictionary and return only values.

//create a swift Dictionary
let swift_dictionary = [1:"swift1",2:"swift2",3:"swift3"]  

print("Actual Dictionary - \(swift_dictionary)")

//get only values
print("Values in a Dictionary - \(swift_dictionary.values)")

 
Output:

Explanation:

We created a swift dictionary that has three key value pairs.


Finally using the values property, we are returning values.

Conclusion

In this Swift tutorial, we saw how to access the elements from a dictionary. There are four ways to access elements from a swift dictionary. Using a for loop we can get all key-value pairs and using a key, we can get value through [] square brackets. Swift Dictionary supports two properties to return Keys and Values. Keys property will return only keys in an Array and values property will return only values in an Array.

Share Button

Source: linuxhint.com

Leave a Reply