如何在SwiftUI中从数组计算总数,小计和平均值

我是SwiftUI的新手,因此已经坚持了几天。

我有一个可识别的无序数组。 我在父视图中有一个用于该数组的过滤器,并且该过滤器的值向下传递到了子视图中。

我需要根据数组中一个元素的值对元素进行分组,然后根据每个数组元素中设置的另一个值来计算总计,小计和平均值。

需要根据在父视图中将过滤器设置为true或false来更新计算。

我通过在ForEach视图内进行计算,轻松地在JavaScript(反应)中解决了此问题,但不知道如何在SwitUI中解决此问题。

任何帮助都将不胜感激!

struct Student: Identifiable {
    let id: String
    let name: String
    let gradeLevel: Int
    let studyHours: Int
}

class GetStudents: ObservableObject {

    @Published var items = [Student]()

    init() {
        self.items = [
            Student(id: "aa1", name: "Bobby", gradeLevel: 9, studyHours: 2),
            Student(id: "aa2", name: "Sally", gradeLevel: 12, studyHours: 4),
            Student(id: "aa3", name: "Susy", gradeLevel: 11, studyHours: 3),
            Student(id: "aa4", name: "Billy", gradeLevel: 12, studyHours: 5),
            Student(id: "aa5", name: "Jimmy", gradeLevel: 11, studyHours: 1),
            Student(id: "aa6", name: "Johnny", gradeLevel: 10, studyHours: 3),
            Student(id: "aa7", name: "Mikey", gradeLevel: 12, studyHours: 2),
        ]
    }
}

struct StudyTotals: View {

    @ObservedObject var students = GetStudents()

    var onlyUpperClass: Bool

    var studentsFiltered: [Student] {
        switch self.onlyUpperClass {
        case true: return students.items.filter {$0.gradeLevel == 11 || $0.gradeLevel == 12 }
        case false: return students.items
        }
    }

    var body: some View {

        var myArray = getCalcTotals(students: studentsFiltered)

        var rows: Array<String> = []

        return VStack {

            Text("Push Calculated Rows Here??? ")

        }.padding()
    }
}

struct ContentView: View {
    @State private var onlyUpperClass: Bool = false
    var body: some View {
        return VStack {
            StudyTotals(onlyUpperClass: self.onlyUpperClass)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


func getCalcTotals(students: Array<Student>) -> Array<String> {
    let studentArray = students.reduce({ /* here */ })

    var grade: String
    var gradeCount: Int
    var gradeHours: Double? = 0
    var gradeAvg: Double? = 0

    let myArray = ["Count", "Total", "Average"]
    return myArray
}

我需要的是:

enter image description here

评论