
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If an Array is Empty in Swift
In this article, we will learn how to write a swift program to check if an array is empty. Here we use two methods:
Using isEmpty property
Using conditional statements
Method 1: Using isEmpty Property
To check if an array is empty Swift provides an isEmpty property. This property will return true if the given array is empty or return false if the given array is not empty. For example Arr = [3, 5, 6, 4, 6] so isEmpty property will return false.
Syntax
var isEmpty: Bool{get}
Or you can also write it as ?
arrayName.isEmpty
Here using the dot operator, we can access the property. Or we can say to use this property we use a dot operator in between the array name and the property name
Algorithm
Step 1 ? Create an array of any data type.
Step 2 ? Check if the given array is empty or not using isEmpty property and store the result into another variable.
var res1Array = IntArray.isEmpty
Step 3 ? Print the output
Example
In the following example, we check whether the given array is empty or not using isEmpty property.
import Foundation import Glibc // Creating an array of integer type var IntArray : [Int] = [74, 99, 9, 38, 78, 132] // Creating an array without elements var StringArray : [String] = [] // Checking if the given array is empty or not // Using isEmpty property var res1Array = IntArray.isEmpty var res2Array = StringArray.isEmpty print("Is IntArray is empty?", res1Array) print("Is StringArray is empty?", res2Array)
Output
Is IntArray is empty? false Is StringArray is empty? true
Here, in the above code, we create two different types(that is Int and String) of arrays and then check if the given arrays are empty or not using the isEmpty property and display the final result.
Method 2: Using Conditional Statements
An empty element does not contain any value or element means the length of the array is zero. So using a conditional statements we check whether the length of the array is zero or not. If the length of the array is zero then the array is empty. Otherwise not.
Algorithm
Step 1 ? Create an array of any data type.
Step 2 ? Check if the given array is empty or not using conditional statements.
if (IntArray.count == 0)
Step 3 ? If the condition is true print the array is empty.
Step 4 ? If the condition is false print the array is not empty.
Example
In the following example, we check whether the given array is empty or not using isEmpty property.
import Foundation import Glibc // Creating an array of integer type var IntArray : [String] = ["Mona", "Pinu", "Moha"] // Creating an array without elements var StringArray : [String] = [] // Checking if the given array is empty or not // Using conditional statements if (IntArray.count == 0){ print("IntArray is empty") } else { print("IntArray is not empty") } if (StringArray.count == 0){ print("StringArray is empty") } else { print("StringArray is not empty") }
In the following example, we check whether the given array is empty or not using a conditional statement.
Output
IntArray is not empty StringArray is empty
Here, in the above code, we create two different types(that is Int and String) of arrays and then check if the given arrays are empty or not using if statement. In the if statement, we set the condition IntArray.count == 0 which means the length of the IntArray is equal to zero. If the condition is true, then the array is empty. Otherwise not.
Conclusion
So, this is how we can check if the given array is empty or not either using the isEmpty property or without the isEmpty property. But the most optimized solution is to check if the given array is empty using the isEmpty property.