Main image of article Swift Tutorial: Getting Started with Sets

Over the past few years, Swift has grown into a much more fully-featured programming language for building apps for the Apple ecosystem. If you’re learning it for the first time, there are some key things to learn—including sets, which are (like arrays) meant for housing data.

However, sets have a somewhat different purpose than arrays (especially since they’re unordered sets of values, unlike arrays, which are ordered collections), and understanding them is important, no matter what your skill level.

What is a ‘Set’ in Swift?

As mentioned above, set is an unordered list of items. It can be strings (words) or numbers. Sets mostly act like arrays… the differentiator is that sets don’t care about the order the data is in.

When to Use Swift Sets

Simply put, when you’re just concerned with corralling data, use a set. A great example would be ingredients for a recipe app. Each recipe may have a set of ingredients. In a recipe, the procedure needs an order, but the list of ingredients? Not so much.

The same could be said for teams without a hierarchy. If you were assembling a team (but had no interest in defining an order or team leader), a set would be fine. All you need to know is who’s on the team!

Setting a Set

Like arrays, sets are initiated with a ‘let’ or ‘var’ keyword; use the ‘let’ constant when you don’t want to change a set, and the ‘var’ variable if you think you may need to alter your data later. Here’s an example:

let names: Set = [“Jim”, “Jeff”, “Josh”, “Jared”]

Here, we’ve initiated a let constant named ‘names,’ used the colon to let Xcode know we’re about to tell it what kind of constant ‘names’ is, and then typed set to let Xcode know it is, indeed, a set. The equals sign tells Xcode we’re about to tell it what the set contains, and we’ve added string values encapsulated in quotation marks (and separated by commas) within the set itself.

If we wanted to be able to change this set later on, we’d assign it as a var instead of let.

Playing with Sets in Swift

One of the main uses of Swift sets is comparison. With an unordered list, it’s sometimes easy to have the same value in each set. Let’s first add a second team:

let names2: Set = [“Josiah”, “Julie”, “Jeremiah”, “Jim”]

But wait… Jim can’t play for both teams! Instead of reading through the lists manually, you can use the .contains function inside an if statement and the && comparison operator:

if names.contains(“Jim”) && names2.contains(“Jim”) {
print(“Jim can’t play on both teams!”)
}

This highlights an important aspect of sets: Be really accurate with your data! Maybe there are two Jims—Jim V and Jim F—but your set data just says ‘Jim.’ Having a last initial or full name would be really useful here!

In that instance, you could also use the == equals-to operator. If you had ‘Jim V’ in both sets, you’d know you were talking about the correct person, not just any Jim.

You can also set an empty set with the following code:

var testSet = Set<String>()

It’s a touch trickier than arrays, but just as useful. Here we’ve instantiated a variable (because we’ll want to add to it later in our code-base), given it a name (testSet), used the equals operator to tell Xcode we’re about to define it, and then used the set keyword and string in a set of <> brackets.

To add to this empty set, use the .insert operator:

testSet.insert(“Hi”)

Now testSet has one value: “Hi”. You could also add individual characters such as “H” with the <Character> operator, and numbers with <Int>. You can even remove values with the .remove operator.

Sets Matter

These are some basic steps for sets; keep them in mind when coding your next project. They’re handy!

Sets can be really useful when you need a dataset to modify, but not in any specific order. If you were returning random values for a game, for example, a set would be perfect.

And if you’re interested in diving deeper into sets, give Apple’s documentation a look. Once you’ve mastered that concept, check out our info on Swift strings, Swift Package Manager, and the details of Swift 5.1.