Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNiels de leeuw
3,217 PointsSwift : 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!
Niels de leeuw
3,217 PointsWell 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
Java Web Development Techdegree Graduate 30,124 PointsAfter 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
3,217 PointsOkay thanks Daniel... See if I can work out what you mean ;-) Any reason why your method is preferable over mine?
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsDaniel Turato
Java Web Development Techdegree Graduate 30,124 PointsCould you explain it a bit better, by perhaps laying out all the different values you want