How to know about the number of elements in a dictionary in Swift?

You may use the “.isEmpty” or the “.count” property to find out the number of elements in a dictionary, or else, if it’s empty.

Given for both examples is the following dictionary:

var myDict = ["apple": 3, "banana": 5, "orange": 2]

.isEmpty

This example shows how to use the .isEmpty property to check if a dictionary contains no element:

if myDict.isEmpty {
    print("The dictionary is empty.")
} else {
    print("The dictionary is not empty.")
    print("The dictionary has \(myDict.count) elements.")
}

/* Output: 
The dictionary is not empty.
The dictionary has 3 elements.
*/

Let’s take a look at the code

  • if myDict.isEmpty
    • This line checks if the dictionary myDict is empty.
    • If it’s the case, the code inside the curly braces will be executed.
  • print(“The dictionary is empty.”)
    • This line prints the message “The dictionary is empty.” to the console.
    • This line will only be executed if the dictionary is empty.
  • } else {
    • If myDict is not empty, the code inside the second curly braces will be executed.
  • print(“The dictionary is not empty.”)
    • This line prints the message “The dictionary is not empty.” to the console if myDict is not empty.
  • print(“The dictionary has \(myDict.count) elements.”)
    • This line prints the number of elements in the dictionary to the console.
    • The count property returns the number of key-value pairs in the dictionary, which is then printed using string interpolation.

.count

Here’s a second example which shows how to use the “.count” property to get the number of elements in a dictionary:

print("The dictionary has \(myDict.count) elements.")

myDict["grapes"] = 4

print("The dictionary has \(myDict.count) elements after adding a pair.")

/* Output:
The dictionary has 3 elements.
The dictionary has 4 elements after adding a pair.
*/

The code explained

  • print(“The dictionary has \(myDict.count) elements.”)
    • This line uses string interpolation to print the number of elements in `myDict`.
    • “\()” is the string interpolation syntax in Swift. It allows for the evaluation of expressions within string literals.
    • “myDict.count” returns the number of elements in “myDict”.
    • This line of code leads to the following output: “The dictionary has 3 elements.”
  • myDict[“grapes”] = 4
    • This line adds a new key-value pair to the dictionary “myDict” where the key is “grapes” and the value is 4.
    • If the key “grapes” already exists in the dictionary, it updates its value to 4.
  • print(“The dictionary has \(myDict.count) elements after adding a pair.”)
    • This line uses string interpolation to print the number of elements in the “myDict” dictionary after adding a new key-value pair.
    • “myDict.count” returns the number of elements in the dictionary.
    • So finally this line of code leads to the following output: “The dictionary has 4 elements after adding a pair.”

Quick notes

  • The “.isEmpty” property returns a boolean value “true” if the dictionary is empty and “false” if the dictionary has one or more elements.
  • The “.count” property returns the number of elements in the dictionary as an integer value.

Leave a Reply

Your email address will not be published. Required fields are marked *