0

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.

5
  • 1
    For a more "Swift" code: 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. Commented Nov 16, 2020 at 15:34
  • 1
    I think you should forget about your super goal since it is overly complicated and will create a lot of overhead with little or no benefit. Commented Nov 16, 2020 at 15:45
  • @JoakimDanielson: I think so! but that would crazy if it would work? do you have any idea for me that I try? where I should start? Commented Nov 16, 2020 at 15:47
  • 1
    No, you would need to maintain 3 different properties and hence store data twice. A computed property is just as good Commented Nov 16, 2020 at 15:52
  • thanks! a lot! I think there is more costs than benefits! I forget it Commented Nov 16, 2020 at 15:54

1 Answer 1

2
struct Person {
    let name: String
    let age: Int
}

class Model {
    var people: [Person] = []
    var peopleCount: Int { people.count }
    var ages: [Int] { people.map(\.age) }
    var names: [String] { people.map(\.name) }
}

let model: Model = .init()
model.people = [.init(name: "bob", age: 30), .init(name: "Roz", age: 26), .init(name: "man", age: 40)]
print(model.ages)  // "[30, 26, 40]\n"
print(model.names) // "["bob", "Roz", "man"]\n"
model.people.append(.init(name: "Omid", age: 20))
print(model.ages)  // "[30, 26, 40, 20]\n"
print(model.names) // "["bob", "Roz", "man", "Omid"]\n"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.