- Declare and define an enum to manage airport statuses
You can instantly get the status of just about anything and airports are no different. This lab takes you on the wild ride of beautifully displaying airport status information. So have a seat because there will probably be delays.
Open your .xcworkspace
file and take a look at the project navigator. The ViewController.swift
file is where you will be doing all your work. Outlet properties are already hooked up, along with some enum properties.
There is an optional instance property on the class called airportDictionary
of type NSDictionary
. When viewDidLoad()
is called airportDictionary
gets its value by calling a class method defined in the Airport Status
class. This method returns a dictionary containing statuses for five major US airports. Each status includes the airport's current delay information and weather. Click on the AirportStatus.swift
file to take a peek and see all the instance properties. Even though it's not hooked up in this lab, the class is set up to work with information received from the FAA's REST API.
Head back to the ViewController.swift
file. The file uses extensions to organize the different parts of the class. Scroll down to the // MARK: Enums
line to view the extension of enums that are already defined for you including one that is not, // Airport Code Enum
. This is the enum you will make. Let's get started.
- Declare an enum called
AirportCode
of typeString
. - Define cases for the following airport codes: ATL, DFW, JFK, LAX, ORD (keep them in alphabetical order).
Hint: Is there a way to define the cases in one line?
Scroll up to the top of the ViewController.swift
file to view all the instance properties. As mentioned before you will see some enum properties already declared.
- Declare an optional enum property below the existing enum properties called
airportCode
of typeAirportCode
and assign a value of.ATL
(That's one of your enum cases). - Scroll down to
viewDidLoad()
and add a print statement inside the function to print yourairportCode
property. - Build and run
⌘ + R
the application. You should see the optional ending in ".ATL" printed out in the console.
Scroll to the airportDictionary
instance property. You'll notice that the didSet
property observer is defined for you. The code inside the observer unwraps the dictionary, sorts the keys, and grabs the first key value. The keys inside the dictionary are the same names as the AirportCode
cases you defined in your enum. You're going to use the code
constant to initialize your airportCode
property.
- Scroll up to your
airportCode
property and remove.ATL
as the initial value. We will no longer assign an initial value here. - Back inside the
didSet
observer ofairportDictionary
, assign the initial value toairportCode
using the raw value initializer. The initializer takes a string argument. Hint: Check out Apple's documentation and scroll down to the "Initializing from a Raw Value" section.
if let code = sortedKeys.first {
// declare and assign the property
}
- Build and run
⌘ + R
the application. You should still see the optional ending in ".ATL" printed out in the console.
So far you have created an enum but it's not really doing much. airportDictionary
contains all the airport statuses of type AirportStatus
. Each AirportStatus
object has all the information you need to assign values to your view properties but we need to get it out of the dictionary first. Now that airportCode
has an initial value we can use its raw value to access a status in airportDictionary
. Remember that the keys inside the dictionary match the case names of your enum ["ATL": <AirportStatus>]
.
- Expand the declaration of the
airportCode
property to include adidSet
property observer. Use the other properties in the class as a reference. - Inside the
didSet
property observer, assign a value to theairportStatus
property. You can get the value by accessingairportDictionary
. Use the raw value ofairportCode
as the key.
Hint: Enums come with a.rawValue
property.
Hint: You will need to downcast toAirportStatus
when accessing the status from the dictionary. - Build and run
⌘ + R
the application. You should see a big control tower, a pretty moon, and a Delta airplane.
The view has a gesture set up so the user can swipe left to load the next airport status. Build and run ⌘ + R
the application. Swipe left a few times. While the transition might be fun, the information remains the same. You are going to change that! Scroll to your AirportCode
enum to add a new method.
- Add a mutating method called
next()
below your cases. Hint: Check out Apple's documentation and scroll to the section "Modifying Value Types from Within Instance Methods". - Inside the mutating method add a switch statement that switches
self
. The goal is to mutate the current value ofself
and change it to the next value (e.g., changeATL
toDFW
,DFW
toJFK
). The switch should be successive and exhaustive meaning the last case should switchself
back toATL
. - Scroll to the section label
// MARK: Gesture
. Find thechangeStatusWithAnimation()
method. Use yourairportCode
property to call thenext()
method you just defined.
if statusReceived {
// call next method here
UIView.transitionWithView(view, duration: 0.5, options: .TransitionFlipFromRight, animations: nil, completion: nil)
}
When next()
is called, the next airport status is retrieved from the airportDictionary
and the view elements are updated with new information.
- Build and run
⌘ + R
the application. Start swiping left to see the status for each airport. - Test
⌘ + R
the application.