Swift – Check if Dictionary is Empty

To check whether a dictionary is empty in Swift, use its isEmpty property. It returns true when the dictionary contains no key-value pairs and false when the dictionary contains at least one entry.

You can also compare the dictionary’s count with zero. Both approaches produce a Boolean result, but isEmpty states the intent more directly.

</>
Copy
var myDictionary:[keyType:valueType] = [:]

var isDictEmpty1 = myDictionary.count == 0
var isDictEmpty2 = myDictionary.isEmpty

In this syntax, replace keyType and valueType with actual Swift types, such as String, Int, or a custom type. An empty dictionary is created using the [:] literal when its key and value types are already specified.

Check an Empty Swift Dictionary Using count

The count property returns the number of key-value pairs in a dictionary. Therefore, the expression myDictionary.count == 0 evaluates to true when the dictionary has no entries.

main.swift

</>
Copy
var myDictionary:[String:String] = [:]
var isDictEmpty = myDictionary.count == 0
print("isDictEmpty: \(isDictEmpty)")

Output

isDictEmpty: true

Because no key-value pairs have been added, the dictionary’s count is zero and the comparison returns true.

Check an Empty Swift Dictionary Using isEmpty

The isEmpty property is the clearest way to test a Swift dictionary for emptiness. It can be used directly in an assignment, an if statement, or a guard statement.

main.swift

</>
Copy
var myDictionary:[String:String] = [:]
var isDictEmpty = myDictionary.isEmpty
print("isDictEmpty: \(isDictEmpty)")

Output

isDictEmpty1: true

The Boolean variable in this example is named isDictEmpty. Its value is true because the dictionary was initialized without any elements.

Use isEmpty in a Swift if Statement

In application code, you will often check a dictionary inside a conditional statement instead of storing the result in another variable.

</>
Copy
let userScores: [String: Int] = [:]

if userScores.isEmpty {
    print("No scores are available.")
} else {
    print("The dictionary contains \(userScores.count) scores.")
}

Output

No scores are available.

Check a Dictionary After Adding and Removing Values

A dictionary’s isEmpty value changes as entries are inserted or removed. The following example checks the same dictionary at different stages.

</>
Copy
var capitals: [String: String] = [:]

print(capitals.isEmpty)

capitals["France"] = "Paris"
print(capitals.isEmpty)

capitals.removeAll()
print(capitals.isEmpty)

Output

true
false
true

The dictionary starts empty, becomes non-empty after an entry is added, and becomes empty again after removeAll() removes its contents.

Empty Dictionary Versus Dictionary with Optional nil Values

A dictionary is empty only when it has no key-value pairs. A dictionary that contains a key is not empty, even when its value type is optional and the stored value represents nil.

</>
Copy
var settings: [String: String?] = [:]
settings["nickname"] = .some(nil)

print(settings.isEmpty)
print(settings.count)

Output

false
1

The dictionary contains the nickname key, so its count is one. To determine whether a particular optional value is nil, inspect that value separately instead of using isEmpty.

isEmpty or count == 0 for a Swift Dictionary

Use dictionary.isEmpty when you only need to know whether the dictionary has any entries. Use dictionary.count when the number of entries is also needed for another operation. Avoid retrieving the count solely to express an emptiness check when isEmpty communicates the condition directly.

Swift Dictionary Emptiness FAQs

How do I check whether a Swift dictionary is empty?

Use dictionary.isEmpty. It returns true when the dictionary has zero key-value pairs.

Can I use count == 0 to check a Swift dictionary?

Yes. The expression dictionary.count == 0 also checks whether the dictionary is empty. However, dictionary.isEmpty is generally easier to read when no count is otherwise required.

How do I create an empty dictionary in Swift?

Declare the key and value types and assign the empty dictionary literal, as in var values: [String: Int] = [:]. You can also use Dictionary<String, Int>().

Does a dictionary containing an optional nil value count as empty?

No. If the dictionary contains a key-value pair, its isEmpty property is false, even when the dictionary’s value type is optional and the associated value is nil.

How do I check whether a Swift dictionary is not empty?

Negate the property with !dictionary.isEmpty. The expression evaluates to true when the dictionary contains at least one key-value pair.

Swift Dictionary Empty Check Summary

Use isEmpty for a direct Boolean check and use count when you also need the number of entries. In this Swift Tutorial, we checked empty dictionaries using both approaches and used isEmpty in practical conditional examples.