We've talked about a raise in Swift, which is a collection of similar objects.
Now there's a special sub array, if you like, called a set in Swift and you get it in a few other
languages, but not that many, as I recall.
This is called a set because it is literally a set of items.
Again, they're all the same kind of item.
But if you think about what a set is in real life, a set is a collection of things that doesn't have
a repeat.
So if I have a set of baseball cards, then that set has one of each baseball player of whatever team
I'm targeting.
You wouldn't put two of the same player in the set, would you?
So that's a handy way to remember sets.
And here's an example we have var faves is a set of strings, which is rock, classical and hip hop,
which is obviously musical styles.
What if I want to add a fave into this.
Well, faves dot, if you remember ARray's we used append with this.
It's not so straightforward because we use insert now.
The subtle difference between append and insert is that append add something to the end at the end is
close enough to add end to remember insert just puts something in to the set of items.
If that new something is the same as something that's already in the set, there'll be no change to
the size of it and that same thing will only appear once.
Right.
So I'm going to give you an example.
First of all, of a lowercase rock are.
So if we play this, that set now has four items.
If we print it out, we'll be able to see.
Hip hop, classical rock and lowercase rock.
OK, so lower case is obviously different to upper case as far as Swift is concerned.
In some cases that may not be the case in different languages.
That could be different, too.
OK.
What do we want to do?
Well, we'd like to see if faves contains rock faves that contains.
Rock, and if we hit play on that, that should give us true because it is actually there.
That's how we check if a set contains a value.
Now, if you were doing a stamp collecting app, for example, you would have this would be ideal for
it because you'd have one of each stamp and you'd query your app and say, do I have this one?
You go into the set and say, no, we don't have that one, so please go and collect it.
Now, we can actually mash these things together, but before we do that, I just want to show you.
What happens if I actually try and insert another?
Lower case rock, and then again, I print.
The favs.
Rock still only appears once that's the lowercase version, because, like I said, if it's already
in there, it doesn't care.
It's just going to not insert that new item for you.
Right.
Let's have a var called rubbish.
What is this?
This is, again, a set of string.
Equal to hip hop.
Now, I'm not saying hip hop's rubbish, I just had to do an example for this.
I like hip hop sometimes, right?
Depending on which song, of course.
And of course, I forgot to put my brackets around.
So now we have a second set, you might have a friend who has a set of stamps and you have a set and
you want to merge them and see what full sets you can come out with using that merge.
And of course, it's the same here.
We can have five dot union.
Faves, rather, this should be plural.
Union with rubbish that's hit play and see what that gives us.
Right, so we've added in hip hop or we haven't, because when we tried to unionise, it already exists.
So what I'm going to do is remove it from this top one.
Now, we should get there we go so it doesn't exist in the original anymore, but when we unión them
together, smash them together, we get it existing, what other things do we have?
We have favs dot intersection.
With rubbish, so this is saying if I bring these two together, what are the similar elements?
And in this case it should be an empty set because nothing is similar.
But if I put in.
That hip hop again.
Now, our intersection should give us hip hop and there we have it as part of that set.
We can also do favors that subtracting.
Returns a new set containing the elements of this set that do not occur in the given sequence.
This is like doing it's like doing the intersection, but bringing back the elements that don't intersect.
And then we have favs dot symmetric difference.
I just copy this to save me from.
Actually writing it, so if we read all these, we should see what happens, it does the same thing,
the symmetric difference, I'm not quite sure what the symmetric differences.
Let's have a look.
Returns the new set with the elements that are either in this set or in the given sequence, but not
in both.
OK, interesting, right?
I've never actually used this.
I've used that once or twice in my life.
But since I have actually used.
Writes my notes over here say you get a homework break, but let's remove that, so I guess there's
no homework for this section.
Lucky you and lucky me, because that's one less video to record.