The isEmpty property checks if the set is empty or not.
Example
isEmpty Syntax
The syntax of the set isEmpty property is:
set.isEmpty
Here, set is an object of the Set class.
isEmpty Return Values
The isEmpty property returns:
- true - if the set does not contain any elements
- false - if the set contains some elements
Example 1: Swift set isEmpty
Output
false true
In the above example, since
- names contain three string elements, the property returns
false. - employees is an empty set, the property returns
true.
Example 2: Using isEmpty With if...else
Output
Elements: ["Federer", "Thiem"]
Here, the names set is not empty, so the if block is skipped and the else block is executed.