Swift Dictionary isEmpty

The isEmpty property checks if the dictionary is empty or not.

Example


isEmpty Syntax

The syntax of the dictionary isEmpty property is:

dictionary.isEmpty 

Here, dictionary is an object of the Dictionary class.


isEmpty Return Values

The isEmpty property returns:

  • true - if the dictionary does not contain any elements
  • false - if the dictionary contains some elements

Example 1: Swift dictionary isEmpty

Output

false
true

In the above example, since

  • names contain three key/value pairs, the property returns false.
  • employees is an empty dictionary, the property returns true.

Example 2: Using isEmpty With if...else

Output

Elements: ["Ranjy": 50000, "Sabby": 100000]

Here, the employees dictionary is not empty, so the if block is skipped and the else block is executed.