Help with trailing closure errors

I am new to swiftui and have a very small project I am working on to practice passing data between views. I have this error on a form "Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure"

If I comment the first section in the form then I get three errors in the ForEach. If anyone can help explain what is going on and what steps I could take to determine where the problem is coming from I would appreciate the help.

This is the model I created:

import Observation
import SwiftUI

@Model
final class Client {
    var id = UUID()
    var name: String
    var location: String
    var selectedJob: Person
    

    init(id: UUID = UUID(), name: String, location: String, selectedJob: Person) {
        self.id = id
        self.name = name
        self.location = location
        self.selectedJob = selectedJob
    }
    
}
extension Client {
    enum Person: String, CaseIterable, Codable {
        case homeOwner = "Home Owner"
        case contractor = "Contractor"
        case designer = "Designer"
    }
}

@Model
class Enclosure {
    var id = UUID()
    var room: String = ""
    var unitType: String = ""
    
    init(id: UUID = UUID(), room: String, unitType: String) {
        self.id = id
        self.room = room
        self.unitType = unitType
    }
}

This is the detail view where the error is happening:


import SwiftData
import SwiftUI

struct DetailView: View {
    @Environment(\.modelContext) private var modelContext
    @Environment(\.dismiss) private var dismiss
    @Query(sort: \Enclosure.room, order: .forward, animation: .default) private var enclosures: [Enclosure]
    
    @State private var showingAddEnclosure = false
    @State private var showingAddMirror = false
    
    
    @State private var name: String = ""
    @State private var location: String = ""
    
    
    @State private var selectedJob = Client.Person.homeOwner
    
    @State var clients: Client
    
    var body: some View {
        NavigationStack {
            Form {
                Section("Details") {
                    TextField("Full Name", text: Client.$name)
                    TextField("Location", text: Client.location)
                    
                    Picker("Job Type", selection: $selectedJob) {
                        ForEach(Client.Person.allCases, id: \.self) { selected in
                            Text(selected.rawValue).tag(selected)
                        }
                    }
                }
                
                Section("Enclosures") {
                    List {
                        ForEach($clients.enclosures) { enclosure in
                            NavigationLink(destination: EnclosureDetailView()) {
                                    VStack {
                                        Text(enclosure.room)
                                        Text(enclosure.unitType)
                                    }
                            }
                            .swipeActions {
                                Button("Delete", role: .destructive) {
                                    modelContext.delete(enclosure)
                                }
                            }
                        }
                    }
                }
            }
            .navigationTitle("Project Details")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Menu {
                        Button {
                            showingAddEnclosure.toggle()
                        } label: {
                            Text("Add Enclosure")
                        }
                        Button {
                            showingAddMirror.toggle()
                        } label: {
                            Text("Add Mirror")
                        }
                    } label: {
                        Label("Add", systemImage: "ellipsis.circle")
                    }
                }
            }
            .sheet(isPresented: $showingAddEnclosure) {
                EnclosureView()
            }
            .sheet(isPresented: $showingAddMirror) {
                EnclosureView()
            }
        }
        
    }
}



hi,

the error message about "parameter of type 'FormStyleConfiguration'" does not give the precise cause of the issue.

in short, error messages from the SwiftUI compiler can be confusing and simply not point you to the real issues, especially when you have lots of code defining the view where the nesting levels go pretty deep. in this case, commenting out the first Section will better point you to the problem that exists in the second section.

indeed, the second Section in your DetailView starts with ForEach($clients.enclosures) { .... but you have not defined an enclosures property on Client, which should be a @Relationship, and Enclosure might want to have an inverse @Relationship property back to Client.

hope that helps,

DMG

Help with trailing closure errors
 
 
Q