7 days of WordPress plugins, themes & templates - for free! Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 30Length: 4.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.2 Sets and Dictionaries

In addition to basic arrays, there are a couple of other important and commonly-used collections types in Swift: sets and dictionaries. In this lesson, I'll introduce the basics of these two collection types, and I'll show you some use cases for them as well.

3.2 Sets and Dictionaries

Now, another data type that is going to follow in the same vein as that is going to be the set. Now basically, the set is going to be very similar to an array, with the exception that at this point, we're talking about a hash or a hash table. If you've ever been familiar with something like that, where we're dealing with unique or distinct values here. So when we're dealing with sets the values going into them are unique. And so, these are very powerful if you wanna do this certain combination checking. And check for unions and intersections and exclusive horizons stuff like that. And I'll show you that in just a moment but let's start off with creating a basic set. Now this is gonna seem fairly similar to what we've done in the past. So let's say I wanted to create a set of numbers. And I could do the same thing that I did before. I could set it equal to set, and then I could do the less than greater than sign. And I could say I'm gonna create a set of integers. And that's fine, I could definitely do that. Then I could also come back and I cannot at this point replace any of this with the square brackets because then Swift would assume that I'm dealing here with an array. And that's really not what we're doing, but what we can do, is we can take this and we can put it back here as the explicit type of numbers here. And paste that in, and I can set this equal to open and close square brackets, and that will work just fine. But one of the nice things about this too, is if I were to initiate this in the same step. And I did one, two, three, say four and five, at this point because I'm specifying and I'm initializing my set with a set of values here that are all integers. I could actually leave off this integer and not be that explicit about it and Swift would be able to figure it out for me. So that's kind of a nice little feature as well as being able to infer the types that are found within the set. So that's kinda nice. And the same thing. Similar type of operations will apply here. I can go into numbers and then I could insert. I could check for the index of a particular number. I could insert another number. I could remove another number. I could remove all numbers. I could remove at an index. So a lot of the same operations exist. So if you're removing something at this point then you're going to have to remember to check to see. Because once again, this is going to be an optional. So you're gonna have to see if that particular value actually exists within the set before you start to do something with it. Otherwise, you could run into some nasty exceptions. Now one of the more interesting things you could do with sets that I started to elude to before is you could start to deal with certain mathematical operations, or certain other types of operations that are fairly interesting. So what I could do is I could create a new set here and we'll call this let odds be a set. And we're gonna set this equal to, we'll just give it some odd numbers here. One, three, five, seven, and nine and then we could say let evens be a set. And we're gonna set that equal to two, four, six, eight and ten. And then we could even create another one here called primes. And we could create this as a set, and we'll set that equal to two, five, and seven. And so basically what we're doing here is we're creating three sets. A set of odd numbers, a set of even numbers, and a set of prime numbers. And this can really sort of set you want. And this is just a very simple example to kind of show you some basic computations here of using sets. So I could do things like, I want to find on odds, I want to find the union of odds with another set here of evens. And at this point, you would get a new set here of one through ten, because it's going to do the union here at this point. So it's gonna give me all of the values associated with odds and union them, or combine them. With all the values for the even, so that's kind of nice. And this is going to squawk at me because it's going to return a value and I'm not actually saving it. So I'll just call this union, just so that goes away. So then what I could do at this point, instead of doing a union, I could also do an intersect. And that's done in a similar way. I could say odds.intersect, and I could see where the odds and the evens intersect. And as you probably already know this is actually going to return an empty set. Because at no point do any of the odd numbers intersect with any of the even numbers. You could also do a subtract which is kind of another interesting operation. So I could say subtract is gonna be equal to odds. And I could do this with the evens. And that would be kind of boring again. But what I could do is I could say at this point I would take the primes, two, five, and seven. And I actually forgot one in here. So we'll throw a three in there also. And if I were to take the primes here out of the odds, I would be left with one and nine. So this would return back a set that would contain one and nine. And then finally, I could do an exclusive or, so I could say x or is going to be equal to odds. And I'll do the same thing again, exclusive or. And I'll do an exclusive or on primes. And obviously what exclusive or is going to mean is it's going to give me all of the values that are found in one or the other but not in both. So I would be returned back one, two and nine. So those are just some nice little functions that are built in that you can work with to start to work with the data that are in two separate sets and see how they relate. And then finally I want to talk about one last collection here. And that is a dictionary. Now a dictionary happens to be one of my favorite types of collection types. Just because I work with it on a fairly regular basis and it tends to suit my needs quite well. And a dictionary is nothing more than a collection of name value pairs. So I basically am imagining to myself, if you could actually find a actual dictionary. And you would think of it that way, that all the words within the dictionary are the keys and they are unique and they are all associated with some sort of value. And, in terms of a dictionary, you're talking about a word and it's definition. So, we can do something very similar to that and switch. So I can create a dictionary here called ages. And I could set that equal to, and the way that we define a dictionary is once again, using the open and close square brackets. But the difference here is that I'm actually going to specify two values. The name and the value type. So I'm gonna create a dictionary that has a string key and an int value. And then once again we'll do open and closed parenthesis, so that we can create this. And then at this point I can start to add, and remove, and get access to things as I wish. So let's actually move this up the screen a little bit to give ourselves a little bit more room. And so what I can do now is I can start to add data in. So how do you add data into a dictionary? Well, you use, once again, the kind of subscript notation here. You use the open and close square brackets, but within here, instead of using an index or an integer, I'm going to specify the name of a key. So, I could say Derek is gonna be my key, and I'm gonna set that equal to 30. So now I have an ages dictionary whose key is a string, and whose value is an integer. And I've created my first key value pair where Derek is set to 30. Now whether or not that is actually accurate or not is really immaterial at this point. But what I can do now is I could go into ages and I could get access to all of the keys I could get access to all of the values. And I could see all the things that are going on within my dictionary and start to do some interesting things with it. But for now we continue to add things to it. Now at this point if I could add in another name, I could say John. And I could set this equal to 20. And this is gonna now have two name value pairs within my dictionary, but if I were to come in here and say ages John is equal to 15. At this point, it's not going to add a 3rd value, it's actually going to over write this second value. So it's just one thing to keep track of because remember the keys within the dictionary are unique. And so, if you specify another one again, it's just going to override that existing version. So if I wanted to clear everything out here, I could simply do it in a similar way that I've been doing before. I could say, ages are equal to open close parenthesis. And I could just at this point, because I've already defined the types that are in here. I could just specify a colon here, then that's going to just clear everything out and give me an empty dictionary. Man I could also check out if it is empty using once again the is empty property. So it's kinda nice to be able to do these types of things, to work with the collection like this and being able to keep track of these name value pairs and this can be a very powerful thing. And we'll start to use this a little bit later on in the class and I'll show you some more examples of how this can be very useful. But for now I think this is good to start. So now you have some collection types in your tool belt that you'll be able to use a little bit throughout your programming within the Swift programming language.

Back to the top