1 00:00:01,010 --> 00:00:04,860 Just like runways, an airport has a finite set of terminals. 2 00:00:04,860 --> 00:00:07,160 So again, let's model it, using a nested enum. 3 00:00:07,160 --> 00:00:10,820 We're going to nest terminal inside control tower, for 4 00:00:10,820 --> 00:00:13,050 the same reason as the runway. 5 00:00:13,050 --> 00:00:16,870 The only object that needs to know about the various terminals in our example 6 00:00:16,870 --> 00:00:18,620 is the control tower. 7 00:00:18,620 --> 00:00:22,650 So, right below the runway type, we'll say enum 8 00:00:24,160 --> 00:00:29,240 Terminal, and create a new type. 9 00:00:29,240 --> 00:00:33,010 Our hypothetical airport will have a few different terminals, so we'll have A, 10 00:00:33,010 --> 00:00:36,510 B, C for domestic flights, and then an international terminal, and 11 00:00:36,510 --> 00:00:38,250 a private hangar. 12 00:00:38,250 --> 00:00:41,890 An airline needs to know both the terminal it will taxi to and 13 00:00:41,890 --> 00:00:43,940 the gate it will park at. 14 00:00:43,940 --> 00:00:47,740 We can define these as separate data points in our landing instructions, but 15 00:00:47,740 --> 00:00:50,340 I think it makes sense to combine them. 16 00:00:50,340 --> 00:00:54,090 Typically, a gate is listed as a ten, where A is the terminal and 17 00:00:54,090 --> 00:00:55,810 ten is the gate number. 18 00:00:55,810 --> 00:00:59,960 So we can list the gate number as an associated value for each enum member. 19 00:00:59,960 --> 00:01:05,300 So we'll say case A, have an associated value, and some optional Int. 20 00:01:05,300 --> 00:01:09,450 I've set a type of optional Int for the associated value, 21 00:01:09,450 --> 00:01:13,520 because we could end up with a situation where the plane is about to land, but 22 00:01:13,520 --> 00:01:15,690 we don't have an open gate yet. 23 00:01:15,690 --> 00:01:18,950 I'm sure you've run into this situation yourself in the real world, 24 00:01:18,950 --> 00:01:22,510 where the plane simply taxis to the terminal and then waits for a gate. 25 00:01:23,650 --> 00:01:26,240 If we don't have an available gate, we can return nil. 26 00:01:26,240 --> 00:01:30,763 So we'll say, case B, and we'll repeat the same thing. 27 00:01:37,803 --> 00:01:38,977 An international terminal. 28 00:01:42,860 --> 00:01:44,651 And finally, we'll say private. 29 00:01:50,347 --> 00:01:54,069 Now the alternative, if we don't have a gate, is to throw an error, but 30 00:01:54,069 --> 00:01:56,600 it's really not an error, is it? 31 00:01:56,600 --> 00:01:59,700 Planes can still land without an open gate. 32 00:01:59,700 --> 00:02:02,340 Now before we can further define the terminal logic, 33 00:02:02,340 --> 00:02:06,090 let's take a quick step back again, and declare an airline type. 34 00:02:06,090 --> 00:02:10,330 Now, this is still not the concrete final type that we're going to use, but 35 00:02:10,330 --> 00:02:15,070 a type that will help us distinguish what kind of airline is coming in. 36 00:02:15,070 --> 00:02:22,499 So, above control tower, I'll say MARK: Airline Type. 37 00:02:23,840 --> 00:02:28,570 In our example, our airport has a few different kinds of flights that come in. 38 00:02:28,570 --> 00:02:33,200 So we have three broad categories, domestic, international, and private. 39 00:02:33,200 --> 00:02:35,970 We have to route each type to a particular terminal. 40 00:02:35,970 --> 00:02:37,660 We've talked about this. 41 00:02:37,660 --> 00:02:41,230 Things, however, get further complicated, because there are several kinds of 42 00:02:41,230 --> 00:02:46,660 domestic flights and they use different terminals based on the fees they pay. 43 00:02:46,660 --> 00:02:50,200 So, let's define an enum to start off with the different domestic 44 00:02:50,200 --> 00:02:52,200 airlines that can land at our airport. 45 00:02:53,540 --> 00:02:56,860 So we'll say enum DomesticAirlineType, 46 00:03:00,840 --> 00:03:05,476 and for this example, we'll say, three different airlines come to our airport, 47 00:03:05,476 --> 00:03:09,880 Delta, American, and United. 48 00:03:09,880 --> 00:03:15,540 So we'll say Delta uses terminal A, American uses B, and United uses C. 49 00:03:15,540 --> 00:03:18,829 Now, remember that empty protocol we created earlier? 50 00:03:18,829 --> 00:03:20,280 AirlineType? 51 00:03:20,280 --> 00:03:24,442 Let's specify that DomesticAirlineType conforms to AirlineType. 52 00:03:28,282 --> 00:03:30,092 Since this is an empty protocol, 53 00:03:30,092 --> 00:03:34,340 it means we don't have to add anything to DomesticAirlineType. 54 00:03:34,340 --> 00:03:36,100 What's even the point, then? 55 00:03:36,100 --> 00:03:41,560 Empty protocols, like AirlineType, are known as marker protocols in Swift. 56 00:03:41,560 --> 00:03:46,810 They allow us to define a higher type to group a few different types. 57 00:03:46,810 --> 00:03:50,530 The airline protocol specifies that conforming types must 58 00:03:50,530 --> 00:03:54,340 indicate the type of airline it is, as you can see here. 59 00:03:54,340 --> 00:03:58,345 This property, the value that we assign, should be of type, AirlineType. 60 00:03:59,520 --> 00:04:02,910 Now, we could have created a single enum called, AirlineType, 61 00:04:02,910 --> 00:04:07,840 and specified every airline that landed at our airport, including domestic, 62 00:04:07,840 --> 00:04:09,840 international, and private. 63 00:04:09,840 --> 00:04:10,990 That's one option. 64 00:04:10,990 --> 00:04:15,070 But doing it this way lets me show you how to use a marker protocol to group 65 00:04:15,070 --> 00:04:16,910 a few different objects to achieve our goal. 66 00:04:17,940 --> 00:04:23,292 Okay, let's add another enum for international airline type. 67 00:04:23,292 --> 00:04:28,177 So I'll say, enum InternationalAirlineType. 68 00:04:28,177 --> 00:04:33,808 Again, conforms to AirlineType, and we'll give a few cases. 69 00:04:33,808 --> 00:04:34,919 So I'll say, case. 70 00:04:40,764 --> 00:04:42,110 You can put whatever you want. 71 00:04:43,370 --> 00:04:47,690 Okay, now that we have our airline types, let's take a small break here. 72 00:04:47,690 --> 00:04:50,810 And in the next video, we'll add a method to our terminal type, 73 00:04:50,810 --> 00:04:54,070 to figure out where the airplane needs to go once it has landed.