Swift Program to Check the dictionary is Empty


This tutorial will discuss how to write swift program to check the dictionary is empty.

A dictionary is used to store data in the form of key-value pair in the collation without any defined-order. Here the type of the keys are same as well as the type of the values are also same. Each key is behave like an identifier for the associate value inside the dictionary and each value has a unique key. Swift dictionary is just like the real dictionary. It loop up values according to their identifier.

Syntax

Following is the syntax for creating dictionary −

var mydict = [KeyType: ValueType]()
Or
var mydict : [KeyType:ValueType] = [key1:value1, key2:value2, key3:value3]

To check the given dictionary is empty or not we can use any of the following method −

  • Using count property

  • Using isEmpty property

Method 1- Using count property

To check the given dictionary is empty or not, we check the size of the dictionary. If the size of the dictionary is 0 then the dictionary is empty otherwise not. So to find the size of the dictionary we use count property.

Below is a demonstration of the same −

Input

Suppose our given input is −

Mydict = [1: “Mona”, 2: “Noh”, 3: “Tarzen”]
NO! The dictionary is not empty
Here Mydict dictionary is not empty because the size of the Mydict is 3

Syntax

Following is the syntax −

dictName.count

Algorithm

Following is the algorithm −

  • Step 1 − Creating dictionaries with key-value pair.

  • Step 2 − Calculating the size of dictionary using count property.

var size1 = mydict1.count
var size2 = mydict2.count
  • Step 3 − If the size is equal to 0, then the dictionary is empty. Otherwise not.

  • Step 4 − Print the output

Example

The following program shows how to count the size of the dictionary.

import Foundation import Glibc // Creating dictionaries var mydict1 : [Int:Int] = [2:34, 4:56, 8:98, 5:3803, 6:23] var mydict2: [Int:String] = [:] // Calculating the size of dictionaries var size1 = mydict1.count var size2 = mydict2.count // Checking if the given dictionaries are empty or not if (size1 == 0){ print("mydict1 is empty") } else{ print("mydict1 is not empty") } if (size2 == 0){ print("mydict2 is empty") } else{ print("mydict2 is not empty") }

Output

mydict1 is not empty
mydict2 is empty

Here, in the above code, we have two dictionaries: mydict1 and mydict2. Now we check they are empty or not using count property. So first we find their size using count property.

var size1 = mydict1.count
var size2 = mydict2.count

Now we check they are empty or not −

if (size1 == 0) // Condition is false{
   print("mydict1 is not empty")
}
if (size2 == 0) // Condition is true{
   print("mydict2 is empty")
}

Method 2- Using isEmpty property

To check the given dictionary is empty or not, we use isEmpty property. This property will return true, if the given dictionary is empty. Otherwise, it will return false.

Below is a demonstration of the same −

Input

Suppose our given input is −

Mydict = []
YES! The dictionary is not empty

Here, Mydict dictionary is empty so isEmpty will return true.

Syntax

Following is the syntax −

dictName.isEmpty

Algorithm

Following is the algorithm −

  • Step 1 − Creating dictionaries with key-value pair.

  • Step 2 − Check if the given dictionary is empty or not using isEmpty property.

mydict1.isEmpty
  • Step 3 − Print the output

Example

The following program shows how to count the size of the dictionary.

import Foundation import Glibc // Creating dictionaries var mydict1 : [Int:String] = [1: "CAR", 2: "BUS", 3:"BIKE"] var mydict2: [String:String] = [:] // Checking if the given dictionaries are empty or not if (mydict1.isEmpty == true){ print("mydict1 is empty") } else{ print("mydict1 is not empty") } if (mydict2.isEmpty == true){ print("mydict2 is empty") } else{ print("mydict2 is not empty") }

Output

mydict1 is not empty
mydict2 is empty

Here, in the above code, we have two dictionaries: mydict1 and mydict2. Now we check they are empty or not using isEmpty property.

if (mydict1.isEmpty == true) // Condition is false{
   print("mydict1 is empty")
}
if (mydict2.isEmpty == true) // Condition is true{
   print("mydict2 is empty")
}
else{
   print("mydict2 is not empty")
}

Updated on: 20-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements