Home > Blockchain >  swiftui list for each cannot display the items in each array
swiftui list for each cannot display the items in each array

Time:06-05

im working on a project and i need to display some arguments from a custom view from a array in a list. But i get this error: Instance method 'appendInterpolation(_:formatter:)' requires that '[Int]' inherit from 'NSObject'.

Here is my code. I hope somebody can help me:

class vm: ObservableObject {
   @Published var filteredArray: [StudentView] = []

   init() {
      getusers()
   }

   func getUsers() {
      let user1 = StudentView(countall: {}, name: "Nick", averages: 0.00, grades: [1, 2, 
      2, 5, 6], getsheet: {})
      let user2 = StudentView(countall: {}, name: "Tim", averages: 0.00, grades: [1, 2, 
      2, 5, 2], getsheet: {})
   }
}


List {
   ForEach(vm.filteredArray) { studentgrade in
      Text("\(studentgrade.grade)")
   }
}

CodePudding user response:

First of all if you need display you should use other struct with view Protocol

I writed for you

import SwiftUI
struct ContentView: View {

@EnvironmentObject var viewModel: vm
var body: some View {
    List {
        ForEach(viewModel.filteredArray) { student in
            HStack{
                ForEach(student.grades, id: \.self){ grade in
                    Text("\(grade)")
                }
              }                                
           }            
       }
   }
}

Text structure does not directly display the array. You have to use ForEach again in HStack to print the grade array side by side

  • Related