How to check if an array is empty in Swift
In this tutorial, we are going to learn about how to check if the array is empty or not in Swift.
Checking array is empty
To check if a array is empty or not, we can use the built-in isEmpty
property in Swift.
The isEmpty
instance property returns true if an array is empty; otherwise returns false.
Here is an example:
var names:[String] = []
print(names.isEmpty)
Output:
true
or we can also check it like this:
var names:[String] = []
if(names.count == 0){
print("names array is empty")
}else{
print("names array is not empty")
}
Output:
"names array is empty"