|
Hello everybody, in the following code, how could you write a makeList() function to replace countryList() and cityList(), given that country and city are properties of a struct Customer let customers: [Customer] = Bundle.main.decode("customers.json") func countryList() -> [String] { var array = [String]() for customer in customers { if !array.contains(customer.country) { array.append(customer.country) } } return array } func cityList() -> [String] { var array = [String]() for customer in customers { if !array.contains(customer.city) { array.append(customer.city) } } return array } |
|
Hi @peacockadoodle
If you put code in
Not sure what you what to do with the properties. |
|
Hi and thank you @NigelGee. I would like to create a general function that could accept the name of a Customer property as an argument, but if I do so, I can't use it in the dot notation.
|
|
Hey, it is not very clear what you are trying to do here, but here's my best guess (correct me if wrong): You would like to have a single function which, when called, returns an array containing a collection of one of the properties of the Customer struct. in other words, the same function will sometimes return an array containing the city of each Customer instance, other times the country property. If that is what you want, then you should be thinking about the function parameter(s) required to make it happen. The question becomes: How do I let the function know which properties to check in the Which you could achieve by using an enum that lists the properties, and then inside your function, you Therefore, your function will need to take that enum as it's parameter. Try it out, and if you still need help with it, do let us know. 😉 |
|
hi Mr. Peacock, rather than inventing many different functions, or trying to unify them with some syntax using a parameter (keypaths come to mind), i would suggest using something more direct at the call site. example: to get a list of the countries represented among your customers, how about pulling out the country values with
if you need the result specifically as an Array, rather than as a Set, you can use
or if you want a sorted array, then
hope that helps, DMG |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.