Previous episode: 30. Challenge: Dictionaries
Next episode: 32. Challenge: Sets
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Now in this exercise, you'll learn about a fourth and final collection type in Swift, and that's sets. Now a set is an unordered collection of unique values, of the same type. So for example, here's a set of Swift birds holding different items, like a game controller, a frying pan with an egg, and an iPhone. But what do I mean by a collection of unique values though? Well, we can insert a Swift bird holding a cat into the set, and that would work. We don't have that image in our set yet, but if we tried to insert another copy of the Swift bird holding the controller, well, nothing would happen. That image is already in the collection and sets can only store unique values. Another way to say this is, sets don't store duplicate values. Now this can be extremely useful when you want to ensure that an item doesn't appear more than once in your collection. Now you can also use sets when the order of your items isn't important. Now let's see how you can use sets in Swift and how they differ from dictionaries. Creating a set is a little bit different than creating an array or a dictionary in Swift. To create a set called "someSet", we have to say, "set" and then add the type inside angle brackets. (keyboard clicking) You can create arrays in dictionaries with a similar looking syntax actually, but both arrays and dictionaries have a shorthand syntax and that's what you've been using all along. Here's what they look like. (keyboard clicking) Now sets just don't have the short option. So you have to explicitly declare them in this form. If you want to initialize this set with some values, that part will look exactly like an array. (keyboard clicking) And now we have a set of ints, but remember I said, sets can't contain duplicate values. So if you check the values on the right, you'll only see a single one. Even though we tried to add the number one to the set twice. When you try to add a duplicate value to a set, Swift simply ignores the duplicate value. Now to find out if a set already contains a certain value, you can use the contains method and just pass in the value you wanna check for. (keyboard clicking) And you'll get a bullion as a result. Now this set does contain one, so we get true. But if you check the set for 99. (keyboard clicking) You'll get false because 99 isn't in the set. If you want to add elements to a set, the insert method is what you're looking for. Now because sets aren't ordered, you don't need to specify where you're inserting the new value. You just pass the value in as the only argument. (keyboard clicking) And now five is part of the set. You can only do this because you've declared the set as a variable. If you have a set that's a constant, you can't add or remove elements. When you do have a variable, like yours here, removing elements is just as easy as adding them. You just use the remove method instead. (keyboard clicking) The remove method actually returns the removed element. So if you wanted to store the element you're removing into a new variable or a constant, that's easy to do. (keyboard clicking) And if it turned out the value wasn't in your set, you'd get nil instead of a value. (keyboard clicking) And you can see that Swift returns nil here to tell me that there was no result from this removal operation. So just like dictionaries, you should use some form of optional binding if you want to use that value. To see what's in our set now, I'm just gonna print out someSet to the console. (keyboard clicking) So you've successfully added five and removed three, leaving you with a set of two, one, and five. Now sets are unordered, so you may see these values in a much different order than I do. In fact, if I run my playground again, (mouse clicks) I'll see them in different order. Now there's one more handy thing I want to show you about sets and that's how to compare them with other sets. So first I'll create another set with at least one value that exists in the first set and at least one value that doesn't. (keyboard clicking) Now there's a few handy methods you can use to create new sets by comparing the two you already have. So you can create a set that only contains elements found in both sets. (keyboard clicking) So that's called intersection, and we can see the result is five. That's the only one in common. There's also symmetric difference, which does the exact opposite of intersection. It returns the elements that are not in both sets. (keyboard clicking) And there's all the values that are not in both. Now the last one is union and this method returns all of the elements in both sets. (keyboard clicking) So there's all my values, but no repeated values. Again because these are sets, you only have one element in the set for any duplicate values. For example, we only have one five in this set from the union. Now these methods all return a new set resulting from each particular operation. But what if you actually want to change one of the sets, in place, in relation to another set? So there are alternate versions of each of these methods that will change or mutate the set you call it on instead of returning a new set. These alternate versions all begin with the word "form". So let's try using form union on someSet and we'll pass in anotherSet as the argument. (keyboard clicking) Now let's take a look at each of these sets and we'll see what happened. I'll just print them out to the console. (keyboard clicking) So you can see here that someSet has actually been muted. It's been changed to include all elements from both sets. AnotherSet hasn't changed at all. Only the set you call the method on, in this case someSet, will change. The set you pass in, in this case, anotherSet, doesn't change, it won't be affected. So that's all I want to show you about sets. Head on into the next video for a set of challenges on, of course, sets.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.