Check if a specific key is present in Swift dictionary
In this tutorial, we will see how to check if a specific key is present in a Swift dictionary. Below are some methods to check if a specific key is present or not.
We can do this in different ways. Here, I am providing four different solutions:
- Using != operator.
- contains(where:).
- contains method.
- first(where:) method.
Let’s explore one by one.
Using != operator
First of all, we have to check whether the appropriate value is nil or not to determine whether a given key is present in a Swift dictionary.
let keyExists = myDictionary[key] != nil
If myDictionary[key] != nil
returns true which means the associated value is not nil, so the given key is present in the Swift dictionary. Otherwise, the key is not present in the dictionary.
Program to check if a specific key is present in a Swift dictionary.
let myDictionary = ["Sam": 22, "Eric": 23, "Nat": 21] let keyExists = myDictionary["Sam"] != nil if keyExists{ print("Key Exists") } else{ print("Key doesn't Exist") }
In the above program, I have created a dictionary called myDictionary
with three key-value pairs. Each key is a String representing a person’s name (“Sam”, “Eric”, and “Nat”), and each value is an Int representing their age (22, 23, and 21).
After that, the expression myDictionary["Sam"] != nil
checks if the returned optional value from the dictionary lookup is not nil, which means the key "Sam"
exists in the dictionary. If the key exists, the variable keyExists
will be assigned a value of true, otherwise, it will be assigned a value of false.
Then, I have used a conditional statement (if
–else
) to print either “Key Exists” or “Key doesn’t Exist” depending on the value of the variable keyExists
. If keyExists
is true
, it will print “Key Exists”, otherwise it will print “Key doesn’t Exist”.
Output:
Key Exists
Using contains(where:) method
Program to check if a specific key is present using contains(where:)
method in a Swift dictionary.
let myDictionary = ["Sam": 22, "Eric": 23, "Nat": 21] let keyExists = myDictionary.contains(where: { $0.key == "Sam" }) if keyExists{ print("Key Exists") } else{ print("Key doesn't Exist") }
In the above program, I have used the contains(where:)
method on the dictionary to check if a key with the string value “Sam” exists in the dictionary. In this case, the condition is that the key of the current key-value pair in the dictionary matches the string “Sam”.
Output:
Key Exists
Using the first(where:) method
Program to check if a specific key is present using first(where:)
method in a Swift dictionary.
let myDictionary = ["Sam": 22, "Eric": 23, "Nat": 21] if let keyExists = myDictionary.first(where: { $0.key == "Sam" }){ print("Key Exists") } else{ print("Key doesn't Exist") }
In the above program, the first(where:)
method accepts a closure as an input and returns a tuple with an optional key-value pair representing the first key-value pair for which the closure returns true. In this case, the closure determines if the current tuple’s key is equal to “Sam”.
Then, the if let
statement unwraps the optional returned by first(where:)
and binds it to the variable keyExists
. If the optional is not nil, the code block inside the if
statement will execute, indicating that the key exists in the dictionary. Otherwise, the code block inside the else
statement will execute, indicating that the key does not exist in the dictionary.
Output:
Key Exists
Using the contains method
We can check if a specific key is present using contains
method in a Swift dictionary. Now have a look at the program below.
let myDictionary = ["Sam": 22, "Eric": 23, "Nat": 21] if myDictionary.keys.contains("Sam") { print("Key Exists") } else { print("Key doesn't Exist") }
In the above program, the keys
property returns a collection containing the keys of the dictionary and the contains()
method checks if the given key "Sam"
exists in the collection of keys.
Output:
Key Exists
Leave a Reply