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