Let's say that I have a class Student
class Student: Identifiable, ObservableObject {
var id = UUID()
@Published var name = ""
}
Used within an Array in another class (called Class
)
class Class: Identifiable, ObservableObject {
var id = UUID()
@Published var name = ""
var students = [Student()]
}
Which is defined like this in my View
.
@ObservedObject var newClass = Class()
My question is: how can I create a TextField
for each Student
and bind it with the name
property properly (without getting errors)?
ForEach(self.newClass.students) { student in
TextField("Name", text: student.name)
}
Right now, Xcode is throwing me this:
Cannot convert value of type 'TextField<Text>' to closure result type '_'
I've tried adding some $
s before calling the variables, but it didn't seem to work.