samedi 11 mai 2019

Filter results in an array using a variable in Swift

I have a collection of car Make and Models and I was wondering if there was a way of returning only the models that belong to certain makes using a variable as a condition? Thank you.

I have tried using if statements but maybe there is another way to accomplish this?

struct Section {

 var make: String
 var model: String!

}

var Cars = [
 Section(make: "BMW", model: ["A", "B", "C"]),
 Section(make: "Ford", model: ["D", "E", "F"]),
 Section(make: "Audi", model: ["G", "H", "I"])
]

var carMake = "BMW"

// aiming to print the models where make = carMake
// for example if carMake = "BMW" the print results would show "A", "B", "C"

Aucun commentaire:

Enregistrer un commentaire