当用户点击该项目时,我想更改与列表中的项目关联的对象的值,并且我收到下一个错误“无法分配给属性:'学生'是'让'常量”。我该如何防止这种情况发生?
这是我的代码:
import SwiftUI
var students: [Student] = load("studentsData.json")
struct StudentsListView: View {
@EnvironmentObject var viewRouter:ViewRouter
var body: some View {
NavigationView{
List(students){student in
Button(action: {
student.isInClass = true
}){
StudentItemListView(student: student)
}
}
}
}
}
struct StudentsListView_Previews: PreviewProvider {
static var previews: some View {
StudentsListView().environmentObject(ViewRouter())
}
}
struct StudentItemListView: View {
var student:Student
var body: some View {
HStack {
Image("profile")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width:50, height:50)
.clipShape(Circle())
VStack(alignment: .leading){
Text(student.firstName + " " + student.lastName)
.font(.headline)
if(student.isInClass == true){
Text("Asistió")
.font(.subheadline)
.foregroundColor(.green)
} else {
Text("Faltó")
.font(.subheadline)
.foregroundColor(.red)
}
}
Spacer()
}.padding()
}
}