How to check if an array is empty in Swift?
Last Updated :
02 Jun, 2022
The Swift language supports different collections like a set, array, dictionary, etc. An array is an ordered generic collection that is used to keep the same type of data like int, float, string, etc., here you are not allowed to store the value of another type. It also keeps duplicate values of the same type. It can be mutable and immutable. When we assign an array to a variable, then that array is known as mutable whereas when we assign an array to a constant, then that array is known as immutable. In the Swift array, we check if the given array is empty or not using the isEmpty property. This property is used to find whether the given array is empty or not. If the given array is empty then this property will return true otherwise it will return false.
Syntax:
arrayName.isEmpty
Here,
arrayName is the object of the array class.
Return Value: This property will return true if the given array is empty or return false if the given array contains elements.
Example 1:
Swift
import Swift
var GEmpSalary = [34, 122, 14, 678, 1, 90]
var result1 = GEmpSalary.isEmpty
print ( "Is the GEmpSalary is empty or not?" , result1)
var GEmployee = [ Int ]()
var result2 = GEmployee.isEmpty
print ( "Is the GEmployee is empty or not?" , result2)
|
Output:
Is the GEmpSalary is empty or not? false
Is the GEmployee is empty or not? true
Example 2:
Swift
import Swift
var AuthorName = [ "Tina" , "Prita" , "Mona" , "Guri" , "Om" , "Rohit" ]
if (AuthorName.isEmpty == true )
{
print ( "Yes! the given Array is empty" )
}
else
{
print ( "No! the given array is not empty" )
}
|
Output:
No! the given array is not empty
Share your thoughts in the comments
Please Login to comment...