Chapter 10, Adding an action sheet

In the section in Chapter 10 on adding an action sheet, there is a line in the code that checks if flight.status is ontime or delayed. However, it is flight.flightStatus, not flight.status that is used to display the rows. FlightStatus knows whether flights have departed or not. The result is that even departed flights will display the “check in for flight” button since the list knows it has departed but the button code for the check in button does not.

If I try to change the lines to check if flightStatus is ontime or delayed, however, the app won’t compile due to a problem with the .leading on the VStack or the padding on the font headline. (Perhaps I am just too much of a newbie to understand why this is so.)

I realize this is just a test app, but it still doesn’t seem right to me when you are trying to only add a check in button for appropriate situations.

In a real app you would have more complex logic so that a departed flight would not be allowed to check in. It was a case of me trying to balance keeping the code somewhat realistic, but not adding too many cases for something that’s not the topic at hand.

My guess on your compile error is that you had a syntax error of some sort. SwiftUI error messages are often pretty misleading and will often flag something like alignment or constant values when the error is somewhere else. The enum for flightStatus is defined as a String so you’d need a comparison that would be something like:

(flight.flightStatus != "Cancelled" && flight.flightStatus != "Departed")

You just need to make sure you check the direction and the status for each side of the OR statement, I changed it to this to get expected behaviour:

(flight.direction == .departure && flight.flightStatus == “On Time”) || flight.direction == .departure && flight.flightStatus == “Delayed”

It is not pretty but it works for this example.

@wrtiede @chris_shay

A clarification as I think I misunderstood the original question a bit. Some of the confusion here I think does come in that this is a sample object without the robustness of a “real” date would have. The status property is an enumeration that holds the status of the flight. flightStatus a computed string property that uses status to display a more user-friendly summary of the status.

It’s simpler to say here that you want to allow check-in if flightStatus shown to the user is “On Time” or “Delayed”. Chris’s comment does take that into account, but can be simplified a bit by pulling the direction check out:

if flight.direction == .departure &&
  (flight.flightStatus == "On Time" || flight.flightStatus == "Delayed") {

I’ll make a note to clarify this in the next book update.