Failed to product diagnostic error

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.

Accepted Reply

Yes, you are doing something (in fact several things) wrong. But to tell you what, you should show the complete Rosa structure.

However, some problems:

    @State private var selezioneGiocatore: Rosa.ID? = nil

Selection must be a Set, as

    @State private var selezioneGiocatore: Set<Rosa.ID> = []
                    TableColumn(Text("Età").foregroundStyle(.blue)) {
                         Rosa in
                        Text("\(Rosa.etàGiocatore)")
                    }

Using the structure name (Rosa) as the argument of the closure is incorrect.

                    Rosa.testRosa().append(nuovoGiocatore)

How is testRosa defined ? Seems to be a func ? append is trying to modify the structure, which is forbidden.

You should call it on an instance as:

                    var rosa = Rosa.testRosa()
                    rosa.append(nuovoGiocatore) 

So please show complete code.

Replies

That generally means there is an error that the compiler can't handle. I've had it due to syntax errors. Also due to complexity of the view.

I would suggest commenting out elements of the view till it works.

Yes, you are doing something (in fact several things) wrong. But to tell you what, you should show the complete Rosa structure.

However, some problems:

    @State private var selezioneGiocatore: Rosa.ID? = nil

Selection must be a Set, as

    @State private var selezioneGiocatore: Set<Rosa.ID> = []
                    TableColumn(Text("Età").foregroundStyle(.blue)) {
                         Rosa in
                        Text("\(Rosa.etàGiocatore)")
                    }

Using the structure name (Rosa) as the argument of the closure is incorrect.

                    Rosa.testRosa().append(nuovoGiocatore)

How is testRosa defined ? Seems to be a func ? append is trying to modify the structure, which is forbidden.

You should call it on an instance as:

                    var rosa = Rosa.testRosa()
                    rosa.append(nuovoGiocatore) 

So please show complete code.

Thanks a lot Claude31!

Above all thanks for your patience in explaining me - I admit to have started just few weeks ago to develop in SwiftUI so I'm conscious of my limits.

Here the code for the Rosa structure:

import Foundation

struct Rosa: Identifiable, Codable, Hashable, Equatable {
    var id = UUID()
    let stagione: String
    let nomeGiocatore: String
    let cognomeGiocatore: String
    let nascitaGiocatore: String
    let etàGiocatore: Int
    let ruoloGiocatore: String
    
    
    
    init(stagione: String, nomeGiocatore: String, cognomeGiocatore: String, nascitaGiocatore: String, etàGiocatore: Int, ruoloGiocatore: String) {
        self.stagione = stagione
        self.nomeGiocatore = nomeGiocatore
        self.cognomeGiocatore = cognomeGiocatore
        self.nascitaGiocatore = nascitaGiocatore
        self.etàGiocatore = etàGiocatore
        self.ruoloGiocatore = ruoloGiocatore
        
        let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "dd-MM-yyyy"
        guard let birthDate = dateFormatter.date(from: nascitaGiocatore) else { return}
                
                let currentYear = Calendar.current.component(.year, from: Date())
                let birthYear = Calendar.current.component(.year, from: birthDate)
        _ = currentYear - birthYear
        
    }
    
    static func testRosa() -> [Rosa] {
        
        return [Rosa(stagione: "2023/2024", nomeGiocatore: "Matt", cognomeGiocatore: "Bar", nascitaGiocatore: "31-03-2000", etàGiocatore: 24, ruoloGiocatore: "Portiere"),
        Rosa(stagione: "2023/2024", nomeGiocatore: "Fabio", cognomeGiocatore: "Bel", nascitaGiocatore: "30-11-1982", etàGiocatore: 41, ruoloGiocatore: "Difensore"),
        Rosa(stagione: "2023/2024", nomeGiocatore: "Ale", cognomeGiocatore: "Nev", nascitaGiocatore: "23-04-2001", etàGiocatore: 23, ruoloGiocatore: "Attaccante"),
                Rosa(stagione: "2022/2023", nomeGiocatore: "Matte", cognomeGiocatore: "Repe", nascitaGiocatore: "28-02-1999", etàGiocatore: 25, ruoloGiocatore: "Centrocampista")]
    }
}

I have then indeed applied all your suggestions, even if the second one "Using the structure name (Rosa) as the argument of the closure is incorrect." is not clear to me. Would you be so kind to help me in understanding it?

Thanks, A.

I think the main error comes from a missing NavigationStack in RosaView.

And you should have rosa

In addition, I had to test on an iPad in order for Table to show columns names and all columns (see here: https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column)

Finally, I made a few changes before it works ok. Please test and tell if that works for you, otherwise, explain what is failing.

struct Rosa: Identifiable, Codable, Hashable, Equatable {
    var id = UUID()
    let stagione: String
    let nomeGiocatore: String
    let cognomeGiocatore: String
    let nascitaGiocatore: String
    let etàGiocatore: Int
    let ruoloGiocatore: String
    
    init(stagione: String, nomeGiocatore: String, cognomeGiocatore: String, nascitaGiocatore: String, etàGiocatore: Int, ruoloGiocatore: String) {
        self.stagione = stagione
        self.nomeGiocatore = nomeGiocatore
        self.cognomeGiocatore = cognomeGiocatore
        self.nascitaGiocatore = nascitaGiocatore
        self.etàGiocatore = etàGiocatore
        self.ruoloGiocatore = ruoloGiocatore
        
        let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "dd-MM-yyyy"
        guard let birthDate = dateFormatter.date(from: nascitaGiocatore) else { return}
                
                let currentYear = Calendar.current.component(.year, from: Date())
                let birthYear = Calendar.current.component(.year, from: birthDate)
        _ = currentYear - birthYear
        
    }
    
    static func testRosa() -> [Rosa] {
        
        return [
            Rosa(stagione: "2023/2024", nomeGiocatore: "Matt", cognomeGiocatore: "Bar", nascitaGiocatore: "31-03-2000", etàGiocatore: 24, ruoloGiocatore: "Portiere"),
            Rosa(stagione: "2023/2024", nomeGiocatore: "Fabio", cognomeGiocatore: "Bel", nascitaGiocatore: "30-11-1982", etàGiocatore: 41, ruoloGiocatore: "Difensore"),
            Rosa(stagione: "2023/2024", nomeGiocatore: "Ale", cognomeGiocatore: "Nev", nascitaGiocatore: "23-04-2001", etàGiocatore: 23, ruoloGiocatore: "Attaccante"),
            Rosa(stagione: "2022/2023", nomeGiocatore: "Matte", cognomeGiocatore: "Repe", nascitaGiocatore: "28-02-1999", etàGiocatore: 25, ruoloGiocatore: "Centrocampista")
        ]
    }
}

// Run on iPad: See https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column
struct RosaView: View { 
    
    @State var rosa: [Rosa] = Rosa.testRosa()
    @State private var apriNuovoGiocatore = false
    @State var stagione: String = "2023/2024"
    var rosaFiltrata: [Rosa] {
        rosa.filter {   // <<-- replace Rosa.testRosa()
            $0.stagione == stagione
        }
    }
    
    @State private var selezioneGiocatore: Set<Rosa.ID> = []
    @State private var ordine = [KeyPathComparator(\Rosa.ruoloGiocatore)]

    var body: some View {
        
        NavigationStack { // <<-- ADD THIS
            VStack(alignment: .leading) {
                Text("Stagione: \(stagione)")
                    .fontWeight(.bold)
                    .font(.headline)
                    .foregroundColor(.blue)
                    .padding()
                
                Table(rosaFiltrata, selection: $selezioneGiocatore, sortOrder: $ordine) {
                    TableColumn("Nome", value: \.nomeGiocatore)  // REMOVE Text( and .foregroundStyle(.blue)
                    TableColumn("Cognome", value: \.cognomeGiocatore)
                    TableColumn("Ruolo", value: \.ruoloGiocatore)
                    TableColumn("Data di nascita", value: \.nascitaGiocatore)
                    TableColumn("Età") {
                        rosa in
                        Text("\(rosa.etàGiocatore)")
                    }
                    .width(100)
                }
            }
            .frame(width: 700, height: 300)
            .toolbar {
                Button {
                    apriNuovoGiocatore = true
                } label: {
                    Image(systemName: "person.badge.plus")
                        .foregroundColor(.blue)
                }
                
                .sheet(isPresented: $apriNuovoGiocatore, content: {
                    NuovoGiocatore(rosaArray: $rosa, nomeNuovoGiocatore: "", cognomeNuovoGiocatore: "", nascitaNuovoGiocatore: "", ruoloNuovoGiocatore: "", etàNuovoGiocatore: 20)  // <<-- pass $rosa as parameter to a Binding
                })
            }
            .navigationTitle("Rosa")
        }
    }
}

struct NuovoGiocatore: View {   // should start with uppercase
    
    @Environment(\.dismiss) var dismiss
    
    @Binding var rosaArray : [Rosa] // <<-- ADD THIS
    @State var nomeNuovoGiocatore: String = ""
    @State var cognomeNuovoGiocatore: String = ""
    @State var nascitaNuovoGiocatore: String = ""
    @State var ruoloNuovoGiocatore: String = ""
    @State var etàNuovoGiocatore: Int = 20
    
    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)
                    // REMOVE THIS var rosa = Rosa.testRosa()
                    rosaArray.append(nuovoGiocatore)
                    dismiss()
                }
            }
        }
        
    }
}