This workshop will be retired on May 31, 2020.
Type Casting4:25 with Pasan Premaratne
Switch statements allow us to pattern match on specific subtypes when using a higher type like a protocol. In this video, we use a switch statement to cast our AirlineType objects to the enums we need so we can conclude our Terminal logic.
Like with the Runway enum, let's add a static method to 0:00 Terminal that takes an Airline and returns a Terminal value. 0:04 So, right here, we'll say, static func terminal. 0:08 Again, it takes an airline of type Airline and returns a Terminal value. 0:15 Inside the body of the method we're going to use a switch 0:22 statement to determine which kind of airline it is first, and 0:25 then do some casting to further determine which particular airline it is. 0:29 So here's how it's going to go. 0:34 We switch on the airline.type since this 0:36 information is encoded into the protocol. 0:40 If the type for the airline.type. 0:45 So if the time returned by this was an enum, 0:48 we could switch directly on the values, but it's not. 0:51 The airline's type property returns a value that is a protocol. 0:55 You can option click to check it out, that's a protocol. 0:59 Switch statements are quite powerful, as we've discussed many times. 1:03 And we can use it to figure out whether the higher protocol type 1:07 is a more concrete subtype. 1:10 For our first case, 1:13 we're going to check if the value is of type DomesticAirlineType. 1:15 So we'll say case is DomesticAirlineType. 1:19 Now, inside the case statement, since we're sure it's a domestic airline, 1:24 we can cast it to a more specific type. 1:29 So we'll say, let domesticAirline = 1:32 airline.type as DomesticAirlineType. 1:37 Notice that I'm using the forced downcast operator here, 1:44 because we only execute this code if the value is of the right type. 1:48 DomesticAirlineType, the type that we're casting to, is an enum. 1:54 So now we can switch further if we want 1:58 to execute code depending on which specific enum value it is. 2:01 So inside this case I'm going to create a new switch statement and 2:05 switch on domesticAirline. 2:09 And now, inside here I can say case .Delta do something. 2:13 At this point, we know which particular airline it is. 2:19 Okay, so the next step now that we know the airline, 2:22 is to return the correct terminal and an available gate for the airline. 2:25 We know that Delta uses terminal A so we can return .A. 2:30 But what about the gate? 2:35 We have a bit more work to do for that, so 2:37 for now, let's just return the right terminal but not worry about the gate. 2:39 So inside as an associate value, we'll say nil. 2:43 American is going to park at Terminal B. 2:48 And United Is going to park at C. 2:54 So, this was our first case in the outer switch, the DomesticAirlineType. 3:04 We have nearly all the logic implemented for domestic airlines. 3:10 The next case we want to check for is an international airline. 3:14 So the inner switch ends over here. 3:18 So now, outside that, I can say, case is InternationalAirlineType. 3:21 So now when we pass in a value as an argument to terminal, 3:28 it's going to go through and check which concrete subtype it is. 3:32 So in this case, 3:37 in the case of InternationalAirlineType, the airport is pretty simple. 3:38 It doesn't matter what airline lands internationally. 3:42 It always goes to the same terminal. 3:45 So in here we can return .International(nil). 3:47 For the default case, if it's neither domestic nor international, we're going to 3:52 just assume it's a private plane, so we'll send it to the private hangar. 3:57 So we'll say default return .private nil. 4:01 Simple enough, right? 4:08 And so far we still don't have a proper airline object just yet. 4:10 Things are starting to come together. 4:15 And in the next video let's implement the gate logic, 4:17 which is intentionally a bit more complicated than it really needs to be. 4:20
You need to sign up for Treehouse in order to download course files.Sign up