[MUSIC] All right so now that we've looked at how to create basic and raw value type enumerations, let's see how we can actually use enumerations. When we're working with enumerations, either if they're of basic enumeration type or a raw value type enumeration, we often can use switches to eliminate a lot of if-else style of programming. And the other benefit of using switches is that switches are exhaustive. So one good thing is that enums and switches work really well hand in hand. In the enum, we list out all the possible cases and in the switch, we can make sure that we have provided the behavior that our program should perform. For every possibility of that enum and the compiler will help us, when we use a switch if we forget a case or if we are are using an associated raw value type for our enum. For example with int, we may have only listed out cases in our enum for int values of zero, one, two and three. But of course there are many more ints than just zero, one, two and three. So we'll learn how to use a default statement to take care of. All the other possible ints that could come in to that enum and make sure that our switch is exhaustive. That way we've guaranteed that we provided a behavior for every possibility in that enum. All right, we can use the init with raw value initializer on any enum type that has a raw value. To convert raw values from just their normal int, or string value, or whatever your raw value type is, into an instance of that enum case. So we'll see that in the playground, keep in mind that the type may be inferred when assigning or comparing enumerations. So, again, like we saw, we can often just say dot and then the name of the case instead of having to write the name of the eNum dot the name of the case, as we've seen earlier. And let's go to the playground and see this in action. All right, so let's look at using enumerations. Here I have a function called showman count inside of my. In numeration, Milky Way planet, we'll talk about multiplying methods on an enum type in another lesson. But for now, let's take a look at how we can use switch statements to work with enums. Okay, we could have created an array of moons and asked for the count of that array. We could have also had a moon count computed property to do that for us. But because we can't actually store the arrays of moons into a property on the enum type, I showed you earlier that enum types do not support storage properties. Then we would of had to type up the arrays of moons once inside of our showMoonCount function and, as we'll see in just a moment, we would have had to type it up in our showMoonNames function again. So that's one of the limitations of enumerations is that we can't hold all of the data for the object that we're modeling in the single code object because stored properties are not supported. All right, so in order to use emos to represent the planned data that we've been. Working within the examples so far, we'll break that information out into these different functions. Okay, so let's look at how this showMoonCount function works. So we have a switch, and notice how we have the switch keyword followed by the value, excuse me, the object that will be switching over. In this case we'll be switching over self, now because this function, show moon count is inside of MilkyWayPlanet itself here as an instance of the MilkyWayPlanet type. So in other words it's going to be a planet. So I can say switch over self and if self is of the case Mercury. Then print 0 because we're trying to show the Moon count and Mercury has no moons. So I'll list out all of the different cases that might supports. Now, notice that Milky Way planet is an int type and here I've list out the cases for mercury, venus, earth, mars, Jupiter, Saturn, Uranus and Neptune. Now that's fine, even though there are more ints than 0 through 7 we have eight cases here, but it's going to start at 0 so 0 through 7 gives us eight cases even though there are more numbers above 7 since I've referenced self, I'm just going to care about the actual cases themselves and not their associated int values. So this switch here is exhaustive because I've listed out a case for each of the eight different cases that are enumerated AKA listed out inside of my e-mail. Okay, so now I've just gone and hardcoded in the number of moons that we know exist for each of these planets, and I'll later be able to show SomePlanet.ShowMoonCount and get this value printed out in my console. Let's look at another switch. So here I have show moon names function. Again this is a function inside of the milky way planet enum or we say it's a method on the milky way planet type, because it comes inside of the curly braces that define that type. All right, so let's look at this second switch. Now, here I have a switch, and I'm switching over self, again. Here I've done something different. I looked at the case, Milky Way planet with rawValue of zero. So normally we wouldn't do this but I wanted to make the point that since we have an associated rawValue type, I can actually, excuse me, I can actually create an instance of MilkyWayPlanet to be Represent mercury. And then if self is equal to mercury the switch will fall into this case of the switch, and of course again we know mercury has no moons. Let me just make one other point here, I put an exclamation point because there may not be a case to find for raw value type of zero, in our case there is. We know that there is because we just created that ENUM, but we may not get a valid result from this initializer. If for example we typed init with a raw value of 8, well we know our raw values only go from 0 through 7, so if I had put 8 here the result of that initializer would be nill. So, I put this exclamation point here to say that the raw value, initing with a raw value of zero, I know is definitely going to give me mercury, so go ahead and force unwrap that value and treat it as a not nil valid value. And we know, because we're the developer, that it will come out to be mercury. Okay, now look at what else is going on here in this switch. I have the case for Venus, a case for Earth. We know Earth's moon is called Luna, so I can show the moon name, that's what this function is trying to do. I have Mars and Neptune, but notice that I have left out a couple of planets. To make sure that this switch is exhaustive, I've put in a default case here. Let me show you why. If I comment out the default case for a moment, the compiler will complain at me, and it will say switch must be exhaustive, consider adding default clause. All right, so that means that there are more cases in the enum that were switching over than I've listed out cases for in the switch. So, I could either do what I did up here and make sure that I list out every single different case, or I can do what the compiler is suggesting. I don't want to say, it's not recommending that I do, it's just suggesting it, in putting your default clause. So, I have said, in other words, if you are not Mercury, we know that this evaluates to Mercury, we just talked about that. So in other words, if you're not Mercury, or you're not Venus, or Earth, or Mars, or Neptune, then let's go into this default case and say this planet has too many moons to list, because some of the planets in the Solar System we know have 27, 50, 53 moons, and for the sake of this presentation, and also in a real application it might not really make sense to list out that much information. You have to decide what is right for your implementation. My point here is to show you how to use. A default case to satisfy that requirement that you're switch be exhaustive. [MUSIC]