
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift Array - isEmpty Property
The isEmpty property of the Array is used to check whether the given array is empty or not. It is the most commonly used property of Array. It checks whether the given collection is empty or not instead of checking the size of the property. The complexity of this property is O(1).
For example, we have an array of integer types: Array = [2, 4, 5]. Now using isEmpty property we check if the array is empty or not. So this property returns false which means the given array is not empty. If the array is empty, then it will return true.
Syntax
Following is the syntax of the isEmpty property −
var isEmpty: Bool{get}
Return Value
This method returns a boolean value. If the given array is empty, then it will return true. Or if the given array contains some value, then it will return false.
Now we will discuss the usage of the isEmpty property with the help of the following examples:
Example 1
Swift program to check if the given array is empty or not with the help of the isEmpty property.
import Foundation // Input array of integer type let arr: [Int] = [] // Checking if the given array is empty or not if arr.isEmpty == true { print("Yes the given array is empty") } else { print("No the given array is not empty") }
Output
Yes the given array is empty
Example 2
Swift program to check how the isEmpty property works with different data types.
import Foundation // Input array of integer type let arr1 = [3, 5, 6, 2] // Input array of string type let arr2 = ["Mohan", "Sumita", "Sonalika"] // Input array of double type let arr3 = [4.5, 6.7, 88.8] // Checking if the given arrays are empty or not using isEmpty print("Is arr1 is empty:", arr1.isEmpty) print("Is arr2 is empty:", arr2.isEmpty) print("Is arr3 is empty:", arr3.isEmpty)
Output
Is arr1 is empty: false Is arr2 is empty: false Is arr3 is empty: false
Example 3
Swift program to find odd numbers from the given array. Here we first check if the given array is empty or not using isEmpty. If the array is empty, then this function will return nil, otherwise return the array with contains only odd numbers.
import Foundation // Function to find odd numbers func oddNums(arr: [Int]) -> [Int]? { // Checking if the given array is empty or not if arr.isEmpty == true { return nil } // Finding the odd numbers let oddNumbers = arr.filter { $0 % 2 != 0 } return oddNumbers } let inputArr = [3, 6, 1, 9, 7] if let result = oddNums(arr: inputArr) { if result.isEmpty { print("Array is empty") } else { print("Odd numbers are: \(result)") } } else { print("Array is empty") }
Output
Odd numbers are: [3, 1, 9, 7]
Example 4
Swift program to find the mean of the array elements. Here we first check if the given array is empty or not using the isEmpty property. If the given array is empty, then the function will return nil. Otherwise, it will return the final mean.
import Foundation // Function to calculate the mean of the given array func calculateMean(arr: [Int]) -> Double? { // If the given array is empty return nil guard !arr.isEmpty else { return nil } // Calculating the sum of the array elements let sum = arr.reduce(0, +) // Calculating the mean let mean = Double(sum / arr.count) return mean } let inputs = [30, 45, 32, 10, 19, 40] if let result = calculateMean(arr: inputs) { print("Mean: \(result)") } else { print("Cannot calculate mean because array is empty.") }
Output
Mean: 29.0