Swift – Check if Specific Key is Present in Dictionary

To check whether a Swift dictionary contains a specific key, access the dictionary with that key and compare the result with nil. A dictionary subscript returns an optional value because the requested key may not exist.

Swift Dictionary Key Lookup Syntax

For dictionaries whose value type is not optional, use the following expression.

</>
Copy
let keyExists = myDictionary[key] != nil

The expression returns true when the key is present and false when it is absent. Store the Boolean result in a variable when the program needs to use the check more than once.

Example 1 – Check if Key is not present in Swift Dictionary

In this example, we shall create a dictionary with some initial values and check whether the key "Surya" is present.

main.swift

</>
Copy
var myDictionary:[String:Int] = ["Mohan":75, "Raghu":82, "John":79]

let keyExists = myDictionary["Surya"] != nil

if keyExists{
    print("The key is present in the dictionary")
} else {
    print("The key is not present in the dictionary")
}

Output

The key is not present in the dictionary

The subscript returns nil because the dictionary has no entry for "Surya".

Example 2 – Check if Key is present in Swift Dictionary

This example checks the existing key "John" in the same dictionary.

main.swift

</>
Copy
var myDictionary:[String:Int] = ["Mohan":75, "Raghu":82, "John":79]

let keyExists = myDictionary["John"] != nil

if keyExists{
    print("The key is present in the dictionary")
} else {
    print("The key is not present in the dictionary")
}

Output

The key is present in the dictionary

The expression evaluates to true because "John" is associated with the value 79.

Check a Swift Dictionary Key and Retrieve Its Value

When the value is needed immediately, use optional binding instead of performing a separate Boolean check. The if let statement runs its first branch only when the key exists and its value can be unwrapped.

</>
Copy
let scores = ["Mohan": 75, "Raghu": 82, "John": 79]

if let score = scores["John"] {
    print("John's score is \(score)")
} else {
    print("The key is not present in the dictionary")
}

Output

John's score is 79

This approach avoids looking up the same key again after confirming that it exists.

Check Swift Dictionary Keys When Values Are Optional

If the dictionary stores optional values, the subscript produces a nested optional. In that situation, index(forKey:) makes a key-presence check explicit: it returns an index when the key exists and nil when it does not.

</>
Copy
let responses: [String: String?] = [
    "name": "Mohan",
    "comment": nil
]

let hasCommentKey = responses.index(forKey: "comment") != nil
let hasEmailKey = responses.index(forKey: "email") != nil

print(hasCommentKey)
print(hasEmailKey)

Output

true
false

The "comment" key exists even though its stored optional value is nil. The "email" key is absent.

Using Dictionary.keys.contains in Swift

You can also search the dictionary’s key collection with keys.contains. This form reads naturally when the code is concerned only with keys.

</>
Copy
let scores = ["Mohan": 75, "Raghu": 82, "John": 79]

if scores.keys.contains("Raghu") {
    print("Raghu is present")
}

For a simple lookup in a dictionary with non-optional values, scores["Raghu"] != nil is usually the most direct form. Use index(forKey:) when key existence must be distinguished clearly from an optional stored value.

Common Swift Dictionary Key-Check Mistakes

  • Checking values instead of keys: dictionary.values.contains(...) searches stored values and does not tell you whether a particular key exists.
  • Using the wrong key type: a [String: Int] dictionary must be queried with a String key.
  • Forgetting case sensitivity: the string keys "John" and "john" are different.
  • Repeating the lookup: use if let when both the presence check and the associated value are required.

Swift Dictionary Key-Check QA Checklist

  • Test the code with one existing key and one missing key.
  • Confirm that the lookup key has the same type and spelling as the dictionary key.
  • Use optional binding when the value will be read after the check.
  • For optional value types, verify key presence explicitly with index(forKey:).
  • Do not replace a key check with a search of dictionary.values.

FAQs about Checking Keys in a Swift Dictionary

How do I check if a Swift dictionary contains a key?

For a dictionary with non-optional values, use dictionary[key] != nil. The result is true when the key exists.

How do I get the value while checking the key?

Use optional binding, such as if let value = dictionary[key]. Inside the block, value contains the unwrapped value.

Can I use keys.contains to check a Swift dictionary key?

Yes. dictionary.keys.contains(key) checks the dictionary’s key collection. A subscript check is often shorter when the dictionary has non-optional values.

How should I check a key when the dictionary value is optional?

Use dictionary.index(forKey: key) != nil when you need an explicit key-presence test independent of the optional value stored for that key.

Swift Dictionary Key Check Summary

Use dictionary[key] != nil for a direct presence check, and use if let when the associated value is also needed. For dictionaries that store optional values, index(forKey:) expresses key existence clearly. In this Swift Tutorial, the examples show checks for missing keys, existing keys, value retrieval, and optional dictionary values.