I am trying to change a value that is stored in class through a custom structure using a picker. I am trying to make an app that lists course name and grade. The picker is in a HStack with a name. I am not able to change the value of a particular element, (in this example, grade) using a Picker. I am using Observable class as I have multiple views which need the access the class.
Here is some of my code,
struct courseItem: Identifiable{
var id = UUID()
let name: String
let grade: String
}
@Observable
class Courses{
var items = [courseItem]()
}
struct ContentView: View{
@State private var courses = Courses()
let grades = ["A", "B", "C"]
var body: some View{
List{
ForEach(courses.items){ item in
HStack{
Text(item.name)
Spacer()
Picker("Grade", selection: item.grade){
ForEach(grades, id: \.self){
Text($0)
}
}
}
}
}
}
}
I know item.grade should be binding but I get an error when I do that. Right now I get an error saying the compiler is unable to type check the expression in reasonable time. Any help is much appreciated.
@Statefor@Observable class, here Managing model data in your app to Create the source of truth for model data@State private var book = Book()and@State private var library = Library(). No examples could possibly illuminate this point with greater clarity. And the example code provided works very well. If you have some other documentation for@Observable classthat shows the opposite, then please provide a link.