Edit Swiftdata data

Dear all, I'm quite new to SwiftUI.

I have a view where I'd like to open a sheet to edit data in a List. Here below the code of the view:

import SwiftUI
import SwiftData

struct SettingsView: View {
    
    @Query(sort: \Stagione.idStagione) private var usoStagione: [Stagione]
    @Environment(\.modelContext) private var context
    @State private var stagione =  String(Calendar.current.component(.year, from: Date()))
    @State private var miaSquadra = ""
    @State private var categoria = ""
    @State private var selStagione = Set<Stagione>()
    @State private var modificaStagione = false
    
    var body: some View {
        NavigationStack {
            GroupBox("Stagione") {
                Form {
                    TextField("Stagione:", text: $stagione)
                        .frame(width: 150)
                    TextField("Categoria:", text: $categoria)
                        .frame(width: 400)
                    TextField("Mia squadra:", text: $miaSquadra)
                        .frame(width: 400)
                    Button("Salva") {
                        let nuovaStagione = Stagione(idStagione: stagione, categoriaStagione: categoria, miaSquadra: miaSquadra)
                        context.insert(nuovaStagione)
                        miaSquadra = ""
                        categoria = ""
                        stagione = String(Calendar.current.component(.year, from: Date()))
                    }
                    .frame(maxWidth: .infinity, alignment: .trailing)
                    .buttonStyle(.borderedProminent)
                    .disabled(categoria.isEmpty || miaSquadra.isEmpty)
                }
                List(selection: $selStagione) {
                    ForEach(usoStagione, id: \.self) { stag in
                        VStack(alignment: .leading) {
                            Text("\(stag.idStagione)").font(.title2)
                            Text("\(stag.miaSquadra)").foregroundStyle(.secondary)
                            Text("\(stag.categoriaStagione)").foregroundStyle(.secondary)
                        }
                    }
                    .contextMenu() {
                            Button(action: {
                                selStagione.forEach(context.delete)
                            }) {
                                Text("Cancella")
                            }
                        Button(action: {
                            self.modificaStagione = true
                        }) {
                            Text("Modifica")
                        }
                    }
                }
                .sheet(isPresented: $modificaStagione) {
                    ModificaStagione(stagione: Stagione)
                }
                .listStyle(.plain)
                .textFieldStyle(.roundedBorder)
                .padding()
            }

        }
        .padding()
        .textFieldStyle(.roundedBorder)
        .navigationTitle("Impostazioni")
        GroupBox("Squadre") {
        }
    }
}

#Preview {
    SettingsView()
        .environmentObject(NavigationStateManager())
        .modelContainer(for: Stagione.self, inMemory: true)
}

While here below the code of the view ModificaStagione:

import SwiftUI

struct ModificaStagione: View {
    
    @Environment(\.dismiss) private var dismiss
    let stagione: Stagione
    @State private var idStagione = ""
    @State private var categoria = ""
    @State private var miaSquadra = ""
    @State private var vistaPrecedente = true
    
    var body: some View {
        VStack (alignment: .leading) {
            GroupBox {
                LabeledContent {
                    TextField("", text: $categoria)
                        .frame(width: 400)
                } label: {
                    Text("Categoria:")
                }
                LabeledContent {
                    TextField("", text: $miaSquadra)
                        .frame(width: 400)
                } label: {
                    Text("Mia squadra:")
                }
                if modifica {
                    Button("Aggiorna dati") {
                        stagione.idStagione = idStagione
                        stagione.categoriaStagione = categoria
                        stagione.miaSquadra = miaSquadra
                        dismiss()
                    }
                    .buttonStyle(.borderedProminent)
                }
            }
            .padding()
            .textFieldStyle(.roundedBorder)
            .navigationTitle("Modifica dati stagione")
        }
        .onAppear {
            idStagione = stagione.idStagione
            categoria = stagione.categoriaStagione
            miaSquadra = stagione.miaSquadra
        }
    }
    
    var modifica: Bool {
        categoria != stagione.categoriaStagione
        || miaSquadra != stagione.miaSquadra
    }
}

In the "Setting" view I receive an error message when I call the view "Modifica Stagione "saying that "Cannot convert value of type 'Stagione.Type' to expected argument type 'Stagione'".

What am I doing wrong?

Thanks in advance, A.

Accepted Reply

If I understand your problem correctly, it would be better to use @Bindable in this structure. More information about Bindable

Using @Bindable

@Bindable var stagione: Stagione

Let's make changes in Textfields and Button

TextField("Categoria", text: $stagione.categoria)
Button("Aggiorna dati") {
     dismiss()
}
.disabled(!modifica)

By doing this, the values in the TextFields will be saved without any function. Please feel free to ask your question :)

Replies

If I understand your problem correctly, it would be better to use @Bindable in this structure. More information about Bindable

Using @Bindable

@Bindable var stagione: Stagione

Let's make changes in Textfields and Button

TextField("Categoria", text: $stagione.categoria)
Button("Aggiorna dati") {
     dismiss()
}
.disabled(!modifica)

By doing this, the values in the TextFields will be saved without any function. Please feel free to ask your question :)