简体   繁体   English

如何修改 SwiftUI 中的列表数据

[英]How to modify List Data in SwiftUI

I want to change the value of a object associated to an item in a List when the user tap in that item and I get the next error "Cannot assign to property: 'student' is a 'let' constant".当用户点击该项目时,我想更改与列表中的项目关联的 object 的值,并且我收到下一个错误“无法分配给属性:'学生'是'让'常量”。 How do I prevente this to happened?我该如何防止这种情况发生?

Here is my code:这是我的代码:

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()
    }
}

As @koen mentioned in the comment, you should be using @State or @EnvironmentObject for accessing/mutating variables and state in your SwiftUI views.正如评论中提到的@koen,您应该在 SwiftUI 视图中使用@State@EnvironmentObject访问/变异变量和 state。

Unlike UIKit, you might notice how SwiftUI relies on the heavy usage of structs.与 UIKit 不同,您可能会注意到 SwiftUI 如何依赖于结构的大量使用。 Structs are immutable by default, this means that you cannot modify any part of a struct without clearly telling the compiler that the struct is mutable (using var keyword, etc.) The way to manage state in SwiftUI is with the use of the Combine framework and the use of @State properties, etc. From a brief look at your code, it looks like all you need to do is add @State var student: Student to your StudentItemListView .默认情况下,结构是不可变的,这意味着您不能在没有明确告诉编译器结构是可变的(使用var关键字等)的情况下修改结构的任何部分。在 SwiftUI 中管理 state 的方法是使用Combine框架以及@State属性的使用等。简要查看您的代码,看起来您需要做的就是将@State var student: Student添加到您的StudentItemListView

If this at all sounds confusing or you are unaware of what @State might be, I highly recommend either watching the 2019 WWDC videos and look at the Apple provided SwiftUI tutorial .如果这听起来令人困惑,或者您不知道@State可能是什么,我强烈建议您观看2019 WWDC 视频并查看 Apple 提供的SwiftUI 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM