This workshop will be retired on May 31, 2020.
Marker Protocols4:54 with Pasan Premaratne
Often times we may use an empty protocol to group a set of unrelated types. In this video, let's get started on building the terminal selection logic using marker protocols and a few enums.
Just like runways, an airport has a finite set of terminals. 0:01 So again, let's model it, using a nested enum. 0:04 We're going to nest terminal inside control tower, for 0:07 the same reason as the runway. 0:10 The only object that needs to know about the various terminals in our example 0:13 is the control tower. 0:16 So, right below the runway type, we'll say enum 0:18 Terminal, and create a new type. 0:24 Our hypothetical airport will have a few different terminals, so we'll have A, 0:29 B, C for domestic flights, and then an international terminal, and 0:33 a private hangar. 0:36 An airline needs to know both the terminal it will taxi to and 0:38 the gate it will park at. 0:41 We can define these as separate data points in our landing instructions, but 0:43 I think it makes sense to combine them. 0:47 Typically, a gate is listed as a ten, where A is the terminal and 0:50 ten is the gate number. 0:54 So we can list the gate number as an associated value for each enum member. 0:55 So we'll say case A, have an associated value, and some optional Int. 0:59 I've set a type of optional Int for the associated value, 1:05 because we could end up with a situation where the plane is about to land, but 1:09 we don't have an open gate yet. 1:13 I'm sure you've run into this situation yourself in the real world, 1:15 where the plane simply taxis to the terminal and then waits for a gate. 1:18 If we don't have an available gate, we can return nil. 1:23 So we'll say, case B, and we'll repeat the same thing. 1:26 An international terminal. 1:37 And finally, we'll say private. 1:42 Now the alternative, if we don't have a gate, is to throw an error, but 1:50 it's really not an error, is it? 1:54 Planes can still land without an open gate. 1:56 Now before we can further define the terminal logic, 1:59 let's take a quick step back again, and declare an airline type. 2:02 Now, this is still not the concrete final type that we're going to use, but 2:06 a type that will help us distinguish what kind of airline is coming in. 2:10 So, above control tower, I'll say MARK: Airline Type. 2:15 In our example, our airport has a few different kinds of flights that come in. 2:23 So we have three broad categories, domestic, international, and private. 2:28 We have to route each type to a particular terminal. 2:33 We've talked about this. 2:35 Things, however, get further complicated, because there are several kinds of 2:37 domestic flights and they use different terminals based on the fees they pay. 2:41 So, let's define an enum to start off with the different domestic 2:46 airlines that can land at our airport. 2:50 So we'll say enum DomesticAirlineType, 2:53 and for this example, we'll say, three different airlines come to our airport, 3:00 Delta, American, and United. 3:05 So we'll say Delta uses terminal A, American uses B, and United uses C. 3:09 Now, remember that empty protocol we created earlier? 3:15 AirlineType? 3:18 Let's specify that DomesticAirlineType conforms to AirlineType. 3:20 Since this is an empty protocol, 3:28 it means we don't have to add anything to DomesticAirlineType. 3:30 What's even the point, then? 3:34 Empty protocols, like AirlineType, are known as marker protocols in Swift. 3:36 They allow us to define a higher type to group a few different types. 3:41 The airline protocol specifies that conforming types must 3:46 indicate the type of airline it is, as you can see here. 3:50 This property, the value that we assign, should be of type, AirlineType. 3:54 Now, we could have created a single enum called, AirlineType, 3:59 and specified every airline that landed at our airport, including domestic, 4:02 international, and private. 4:07 That's one option. 4:09 But doing it this way lets me show you how to use a marker protocol to group 4:10 a few different objects to achieve our goal. 4:15 Okay, let's add another enum for international airline type. 4:17 So I'll say, enum InternationalAirlineType. 4:23 Again, conforms to AirlineType, and we'll give a few cases. 4:28 So I'll say, case. 4:33 You can put whatever you want. 4:40 Okay, now that we have our airline types, let's take a small break here. 4:43 And in the next video, we'll add a method to our terminal type, 4:47 to figure out where the airplane needs to go once it has landed. 4:50
You need to sign up for Treehouse in order to download course files.Sign up