Post marked as solved
Post marked as solved with 4 replies, 237 views
Dear all,
I have the following code in a view:
import SwiftUI
struct RosaView: View {
@State var rosa: [Rosa] = Rosa.testRosa()
@State private var apriNuovoGiocatore = false
@State var stagione: String = "2023/2024"
var rosaFiltrata: [Rosa] {
Rosa.testRosa().filter {
$0.stagione == stagione
}
}
@State private var selezioneGiocatore: Rosa.ID? = nil
@State private var ordine = [KeyPathComparator(\Rosa.ruoloGiocatore)]
var body: some View {
VStack(alignment: .leading) {
Text("Stagione: \(stagione)")
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
.font(.headline)
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
.padding()
Table(rosaFiltrata, selection: $selezioneGiocatore, sortOrder: $ordine) {
TableColumn(Text("Nome").foregroundStyle(.blue), value: \.nomeGiocatore)
TableColumn(Text("Cognome").foregroundStyle(.blue), value: \.cognomeGiocatore)
TableColumn(Text("Ruolo").foregroundStyle(.blue), value: \.ruoloGiocatore)
TableColumn(Text("Data di nascita").foregroundStyle(.blue), value: \.nascitaGiocatore)
TableColumn(Text("Età").foregroundStyle(.blue)) {
Rosa in
Text("\(Rosa.etàGiocatore)")
}
}
}
.frame(width: 900, height: 400)
.toolbar {
Button {
apriNuovoGiocatore = true
} label: {
Image(systemName: "person.badge.plus")
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
}
.sheet(isPresented: $apriNuovoGiocatore, content: {
nuovoGiocatore()
})
}
.navigationTitle("Rosa")
}
}
struct nuovoGiocatore: View {
@Environment(\.dismiss) var dismiss
@State var nomeNuovoGiocatore: String
@State var cognomeNuovoGiocatore: String
@State var nascitaNuovoGiocatore: String
@State var ruoloNuovoGiocatore: String
@State var etàNuovoGiocatore: Int
var body: some View {
NavigationStack {
Form {
TextField("Nome:", text: $nomeNuovoGiocatore)
TextField("Cognome:", text: $cognomeNuovoGiocatore)
}
.navigationTitle("Nuovo giocatore")
.toolbar {
Button("Cancel") {
dismiss()
}
Button("Aggiungi giocatore") {
let nuovoGiocatore = Rosa(stagione: "2023/2024", nomeGiocatore: nomeNuovoGiocatore, cognomeGiocatore: cognomeNuovoGiocatore, nascitaGiocatore: nascitaNuovoGiocatore, etàGiocatore: etàNuovoGiocatore, ruoloGiocatore: ruoloNuovoGiocatore)
Rosa.testRosa().append(nuovoGiocatore)
dismiss()
}
}
}
}
}
#Preview {
RosaView()
}
On this, I'm getting a strange error which is "Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)" in the "var body: some View" statement of the view "nuovoGiocatore".
How can I avoid it? Am I doing something wrong in the coding?
Thanks,
A.