I have a Person
object:
class Person{
var name: String
var city: String
var country: String
init(name: String, city: String, country: String){
self.name = name
self.city = city
self.country = country
}
}
and two arrays. One array of Person
objects and one with the names of each of the attributes of the array.
var values = [Person]()
var attributes = ["name","city","country"]
and for example if I add two new Person
objects to the array:
values.append(Person(name: "Peter" as! String, city: "Paris" as! String, country: "France" as! String))
values.append(Person(name: "Andrew" as! String, city: "Madrid" as! String, country: "Spain" as! String))
I would like to retrieve each of the attributes of each object of the array.
If I want to retrieve all the attributes of the first element I can do:
values[0].name
values[0].city
values[0].country
but I would like to do it in the way:
let totalAttributes = attributes.count - 1
for i in 0...totalAttributes{
var attribute = attributes[i]
values[0].attribute
}
but this, as I supposed, did not work.
In this case I can do it manually because there are only three attributes but I will not like to do the same when I will have more than 20 attributes inside the object.
Is there a way to do it within a loop?
Thanks in advance!