← All languages
Swift function of the day
Random

Dictionary.isEmpty

A Boolean value indicating whether the dictionary has no key-value pairs.

Description

The isEmpty property returns true if the dictionary contains no key-value pairs, and false otherwise. It is preferred over checking count == 0 for clarity.

For Dictionary, both isEmpty and count are O(1), but isEmpty better communicates intent.

isEmpty is available on all Collection types in Swift.

Example

var config: [String: String] = [:]
print(config.isEmpty)  // true

config["theme"] = "dark"
print(config.isEmpty)  // false

Reference