Member-only story
Hi everyone, welcome back. Today, we will be going over some of the most common and useful properties used with sets in Swift. A basic tutorial can be found here. Sets are a common data type that can be used to keep a collection of data within a singular set variable. Sets are unordered and allows only unique values. In this article, we will go over various properties of the set. With this introduction out of the, let’s get into it.
isEmpty
Let’s start with the ‘isEmpty’ property of Swift sets. The ‘isEmpty’ property will return a boolean value stating whether a set is empty or not. Let’s see an example of us using this property on an empty set:
var set = Set<Int>()
print(set.isEmpty)Output:
true
In this example we created and used an empty set. The ‘isEmpty’ property is verifying that our set is empty by returning true in our output. Let’s see what happens if we try this in a populated set:
var set: Set = [42, 23, 67]
print(set.isEmpty)Output:
false
We know that our set is not empty, so we should expect that our output from the ‘isEmpty’ property to be false. Looking at the output above, we can see that we got our expected result.