0

How can I get a maximum value from my object with a let?

var body: some View{
    ZStack{
           ForEach(schoolClasses, id: \.self) { studentList in
  
                 let oldestStudent = studentList.max { $0.age < $1.age } //this does not work, trying to return the oldest student
           
                 HStack{
                  //extra code to use the oldest student          
                 }
          }
       }
   }

How I can use that let value inside the HStack?

Right now it gives me build error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

dhaval123
  • 107
  • 11
  • 1
    Assuming `studentList` is a array of `[Student]`, then try this approach: `let oldestStudent = studentList.students.max { $0.age < $1.age }`. If not, show what `studentList` is made of – workingdog support Ukraine Feb 24 '23 at 23:20

2 Answers2

1

Try this approach, as shown in this example code, to obtain and use the oldest student in a list of Student:

struct StudentList: Hashable {
    var students = [ Student(age: 5), Student(age: 6), Student(age: 8), Student(age: 3)]
    //...
}

struct Student: Identifiable, Hashable {
    let id = UUID()
    var age: Int
    //... name etc...
}

struct ContentView: View {
    @State var schoolClasses = [StudentList()]
    
    var body: some View{
        ForEach(schoolClasses, id: \.self) { studentList in
            if let oldestStudent = studentList.students.max(by: { $0.age < $1.age }) {
                HStack{
                    // Text(oldestStudent.name)
                    Text("oldest student age: \(oldestStudent.age)")
                }
            } else {
                Text("no oldest student available")
            }
        }
    }
}
  • I was wondering if you have a solution for this: https://stackoverflow.com/questions/75561419/transition-on-button-without-toggle-or-withanimation-swiftui – dhaval123 Feb 25 '23 at 11:30
1

Your code implies that schoolClasses is a nested array [[SchoolClass]] which makes no sense.

A reasonable data structure is an array of students in each class

struct Student {
    let age: Int
}

struct SchoolClass: Identifiable {
    let id = UUID()
    var students : [Student]
}

In SwiftUI it's good practice to keep as much computing stuff as possible out of the view.

Add a computed property to SchoolClass

struct SchoolClass: Identifiable {
    let id = UUID()
    var students : [Student]

    var oldestStudent : Student? {
        if students.isEmpty { return nil }
        return students.max { $0.age < $1.age }
    }
}

In the view write

var body: some View {
    ZStack {
        ForEach(schoolClasses) { schoolClass in
            if let oldestStudent = schoolClass.oldestStudent {
           
                 HStack {
                     //extra code to use the oldest student 
                 }         
             }
         }
    }
 }
   
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I was wondering if you have a solution for this: https://stackoverflow.com/questions/75561419/transition-on-button-without-toggle-or-withanimation-swiftui – dhaval123 Feb 25 '23 at 11:30