I am making a struct called personStruct, which takes simple data like age and name. Here is this struct:
struct personStruct
{
var name: String = String()
var age: Int = Int()
}
then I am using this down class for extracting my wished arrays:
class PersonModel
{
var persones: [personStruct] = [personStruct]()
var countOfPersones: Int { persones.count }
func personesAgeArray() -> [Int]
{
var returnArray: [Int] = [Int]()
for item in persones { returnArray.append(item.age) }
return returnArray
}
func personesNameArray() -> [String]
{
var returnArray: [String] = [String]()
for item in persones { returnArray.append(item.name) }
return returnArray
}
}
And this is my use case of those struct and class:
let personModel: PersonModel = PersonModel()
personModel.persones = [personStruct(name: "bob", age: 30), personStruct(name: "Roz", age: 26), personStruct(name: "man", age: 40)]
print(personModel.personesAgeArray())
print(personModel.personesNameArray())
So as you see that class make that Job done!
My Goal: Do we have a real syntax or more efficiency way for making those Arrays? (I mean personesAgeArray and personesNameArray)
My Super Goal: I want personesAgeArray and personesNameArray get automatically builded/updated with adding new item to persones.
func personesAgeArray() -> [Int] { return persones.map{ $0.age }}maybe? But it's still working code to do a manual loop. For the rest, prefers using an uppercase for the first letter in struct and class names (personStruct->PersonStruct), and naming might change.