Swift .isEmpty
Anonymous contributor
Published Dec 1, 2024
Contribute to Docs
.isEmpty is a built-in property in Swift that checks whether a set contains any elements. It returns a Boolean value: true if the set is empty and false otherwise, offering a simple and efficient way to check for an empty collection.
Syntax
setName.isEmpty
setName: Refers to the variable name of the set being evaluated. It represents the specific set whose emptiness is being checked.
Example
This example code shows how to determine whether a set is empty using the .isEmpty property:
// Creating a set with elementsvar numbers: Set<Int> = [1, 2, 3]// Check if the set is emptyif numbers.isEmpty {print("This set is empty.")} else {print("This set contains elements.")}
This example results in the following output:
This set contains elements.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
- Includes 7 Courses
- With Certificate
- Beginner Friendly.13 hours
- A powerful programming language developed by Apple for iOS, macOS, and more.
- Beginner Friendly.12 hours