[MUSIC] Hi, and welcome to the next section on types. Today, we'll be looking at enumerations. Let's look at what we'll be covering. We'll talk about of course what is an enumeration. We also often refer to these as enums for short. We'll look at creating basic and what are called raw value enumerations. We'll look at how to use those, and we'll look at how we can define functions on enumerations. Keep in mind that enumerations are a type, and when we create functions on a type as opposed to just global functions, we refer to those as methods. And lastly we'll talk about some limitations of enumerations compared to other types that we'll be studying about in this course. So, what is an enumeration? Enumerations are a value type. They define a list of possible cases for instances of that type. Keep in mind that when working with types, we're trying to model real world objects or perhaps model elements of our user interface or different, maybe kind of behind the scenes code structures that we've developed to make our actual application work. But the main purpose of types again is to model real world objects. And in this course we'll be also looking at structures and classes as different types that help us model those real world objects. So, often we assign an enum types into instance variables or properties to identify known possible cases. So let's look at creating basic enumerations. We're going to go to the playground again, look for a syntax that defines an enumeration and it's possible cases. Look for syntax for creating an instance of an enum type. And look for how we can rely on Swift's type inference capabilities and not have to do quite so much typing when we use enums. So let's take a look at the playground and learn how to actually create some basic enumerations. Here we have our solar system example again. We're going to be working with a collection of planets. So let's take a look at this solar system with the planets as an enum type. Look at the syntax here. Note that the enumeration is declared with the keyword enum here in pink. And just one note, you may see your code being in different colors depending on your external code settings. But the default, you should see these types of keywords be in pink, all right. The enumeration has the enum keyword followed by the name of the enumeration itself, so this is the planet enum. And notice that we called it Planet and not Planets because we want to just describe what are the different possibilities of each planet. We don't want to talk about Planets as a collection. All right, inside of the curly braces is where we define all of the possible cases, or the known cases for this enum. So, at least in our solar system, the possible planets are Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus and Neptune. So we've created a case for each of those known possibilities. Keep in mind, we're trying to model a real world object, in this case, our solar system. So we want to make a different case for each different possible planet because that's what we're trying to model is the planet. All right, notice the syntax of listing out the cases. We've put the keyword case before each possibility and we put them each on a different line. All right, so now that we have declared the planet enum, let's take a look at how we can actually create instances of the enum type. In other words, let's actually make the planets now. These are only saying what the different planets could be, they're not instantiating any object for us. So let's look at how we do that. We're going to create an array called theSolarSystem. And it's going to be of type, we'll use the array literal syntax with the open square bracket, and then put the type that, that array will accept. So now, before we had our planets existing as just dictionaries which was fine. It was a discreet way to define a planet in code. But now we actually have a type, called planet, so we can actually say, this array will only accept planets or will only accept objects of type planet. Now say that equals an array and into this array I can put instances. So I can come over here and say Planet.Mercury and that will actually create an instance of the planet type of the possible case called Mercury. And then inside of my array, I have all the values separated by commas, and I just continue to list out the planets in order. Because as long as we are working with an array, it makes sense to list them out in some kind of natural order. So the order that the planets are in terms of distance form the sun is how we normally talk about the planets. And I can just list them out, Mercury, and then Venus, Earth, Mars, etc. Notice that for Mercury I said Planet.Mercury. But for all the rest of the planets, I just call them by the different cases using .syntax. This is allowed because we've explicitly typed this array to accept instances of the planet type. If I hadn't done that, I would need to say planet.venus, planet.earth, etc for all the values in there. But because I've explicitly typed this array, it can infer what types of values will be in this array. And then I can just use the shorthand .syntax for all the cases in the enum. [MUSIC]