Swift Dictionary count

The count property returns the number of elements of the dictionary.

Example


count Syntax

The syntax of the dictionary count property is:

dictionary.count 

Here, dictionary is an object of the Dictionary class.


count Return Values

The count property returns the total number of elements present in the dictionary.


Example 1: Swift Dictionary count

Output

3
0

In the above example, since

  • nameAge contains three key/value pairs, the property returns 3.
  • employees is an empty dictionary, the property returns 0.

Example 2: Using count With if...else

Output

Small Company

In the above example, we have created the dictionary named employees with 3 key/value pairs.

Here, since there are only 3 key/value pairs in the dictionary, numbers.count > 5 evaluates to false, so the statement inside the else block is executed.