.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
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours