Swift 4.2 Essentials

Sets

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video covers what are known in Swift as Collections. The first type is called sets. In the video, we will explore how to create and manipulate these types in your code lines.

Keywords

  • Swift 4
  • Ios
  • Tutorial
  • sets
  • arrays
  • dictionaries

About this video

Author(s)
Mark Hoath
First online
17 November 2018
DOI
https://doi.org/10.1007/978-1-4842-4232-2_7
Online ISBN
978-1-4842-4232-2
Publisher
Apress
Copyright information
© Mark Hoath 2019

Video Transcript

Okay, so our next topic is set, and I think around grade four or five you probably did some work with sets of numbers and essentially sets is using that exact same mathematics. Programmatically you‘re probably unlikely to use sets unless you’ve got a very specific requirement. So, a set stores distinct data of the same type in a collection with no defined ordering, and they’re useful if order is not important, and you want to avoid duplicates. So, if you have the name Mark in your set, and then you go to insert another Mark it’s not going to create two options in the sets. There can only be one Mark, and so that insert would have no difference on the set. Now, a type, there’s the type that you’re going to create this set out of. So, it’s an integer, or a string, etc. The type must be hashable in order to be stored in a set, and the type must provide a hash value itself such as that if a was b then hash value of a would equal the hash value of b, and that’s true for all the basic types of the whole string and bull.

So, let’s have a look at the creation, and the initialization of a set. So, if we wanted to create some letters, and we create a set using the key word set, and whatever the data type is that we’re going to use. So, a set of characters, and then to create an empty set we just use an empty initializer, and if we were to print letters is of type set character with letters dot count, and that will give us a total of how many characters are in this set, and the count of those items. Then if you want to actually insert something we just go letters dot insert new characters so we can just type a, which is a character, and if we want to create, or if we’ve got a set that contains something, and we want to empty it then we can just go with letters is equal to that. There might even be a remove all. So, either of those you remove everything, or you just reset letters to be this empty array symbol. Then you end up with an empty set, but the thing you’ve got to remember is even though you’ve taken all the items outside of the set. So, it’s an empty set it’s still a set of type characters. So, you wouldn’t be able to go letters dot insert integer. You would only be able to do it as a string.

So, what else could we do with a set? Well, if we were to add some values. So, let’s create names, which is a set of type string, and we can set off some initial so we can set up and, and we can set up Belinda, and we can add in Christine, and then we can go into lams/g dot removal. You go with that keep capacity there if you want, but we don’t want to keep the capacity. We’re not worried about memory at the moment, and then you can test to see if it is empty. You go names dot is empty, and then we can go names dot insert, and again we can add Anne, and we can add Barry, and we can add Drew, and that would give us three names in our set, and if we want to pass all of those names to get them and find out what was in there we’d go for name in names, and that’s a full loop, and it’s going to go through every item in our name set, and with each item it’s going to allow us to just print the name.

So, that’s how you could go through all of the items in the set in a full loop, and if you did that you may notice, depending on when you do it, because I set up here that order is not important so when you pull a name from names it’s not that you’re pulling the first one, or the second one, or the third one. It’s pulling a random one. So, it’s not going to be in alphabetical order. So, you want it to sort them, and you could go for names dot sorted, and then do the same thing. You then print name, and that would print the names in alphabetical order. So, as I said, I don’t use sets particularly very much. I suppose that if you’ve got some sets that’s in math then if we look at names then you can see that there are these tests that you’ve got. You’ve got contains, and you can take the first item, and does it form the intersection, or does it form a symmetric difference with another set, does it form a union, and is also like subtraction or disjoined, is a strip subset of? So, if you need to do some set arithmetic then all those methods and functions exist, and you’re able to cause Meta functions to find out. So, you can go to names dot union if we don’t have another one, but you go names dot union, and names, and that would give you all the names because they always unite with themselves.