.isEmpty

Anonymous contributor's avatar
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 elements
var numbers: Set<Int> = [1, 2, 3]
// Check if the set is empty
if 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

Contribute to Docs

Learn Swift on Codecademy