Swift

Niels de leeuw
Niels de leeuw
3,217 Points

Swift : data type question

With the knowledge I have so far I'm starting to built an app. It's for use in aviation. I need to be able to access a database with airports and their alternates. So for example airports are AMS, LHR, CDG. Every airport has 3 alternates. So AMS has RTM, EHV & BRU. When I select AMS in a picker of the app, I need the 3 alternates to appear on 3 different labels. Can anyone suggest what would be the best data type here. An of an array array of strings, a struct or an enum? thanks a lot!

Daniel Turato
Daniel Turato
Java Web Development Techdegree Graduate 30,107 Points

Could you explain it a bit better, by perhaps laying out all the different values you want

Niels de leeuw
Niels de leeuw
3,217 Points

Well lets keep it simple. I have 2 airports as destination. Each destination has 3 alternates: So airport AMS has alternates RTM, EHV, EBR airport LHR has alternates LGW, LCY, MCT When I select an airport in my picker (AMS or LHR), i want 3 labels I made to be filled with the 3 alternates that belong to the airport.

I made this (assuming var airportPicker gets its value from a picker), but wondering if it can be done better:

var alt1 = "" var alt2 = "" var alt3 = "" var airportPicker = "AMS"

let airports = ["AMS":["RTM","EHV","EBR"],

               "LHR":["LGW","LCY","MCT"]]

if let alternate = airports [airportPicker] { alt1 = alternate[0]; alt2 = alternate [1]; alt3 = alternate [2] } print (alt1, alt2, alt3)

2 Answers

Daniel Turato
PRO
Daniel Turato
Java Web Development Techdegree Graduate 30,107 Points

After reading the type of values you want, this is what I would do:

  • Create an enum for every airport value
  • Create an Airport struct with a field for the airport enum value

Now , unless alternative values overlap between airports, then you should create a string array field in the Airport struct named alternatives. Then in the init, pass in the airport enum code and the arrayof alternatives. Then you could hard code the various instances of airports needed.

However, if alternative values do overlap then you should probably create another enum named alternatives or something and then instead of a string array do an alternative enum array.

Niels de leeuw
Niels de leeuw
3,217 Points

Okay thanks Daniel... See if I can work out what you mean ;-) Any reason why your method is preferable over mine?