WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure

Forums > SwiftUI

Unfortunetaly I get the error "Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure" and also "'init(:)' declared here (SwiftUI.Form)" in the following code:

import SwiftUI

struct ExpenseLoggingView: View { @State private var date: Date = Date() @State private var description: String = "" @State private var amount: Double = 0.0 @State private var payer: User?

@ObservedObject var group: Group

var body: some View {
    NavigationView {
        Form {
            Section(header: Text("Expense Details")) {
                DatePicker("Date", selection: $date, displayedComponents: .date)

                TextField("Description", text: $description)

                TextField("Amount", value: $amount, formatter: NumberFormatter())
            }

            Section(header: Text("Payer")) {
                // Payer selection
                Picker("Payer", selection: $payer) {
                    ForEach(group.friends) { friend in
                        Text(friend.name).tag(friend as User?)
                    }
                }
            }

            Section {
                Button("Submit Expense") {
                    if let payer = payer {
                        let expense = Expense(id: UUID(), date: date, description: description, amount: amount, payer: payer, participants: group.friends)
                        group.expenses.append(expense)
                    }
                }
            }
        }
        .navigationBarTitle("Add Expense")
        .navigationBarItems(leading: Button("Home") {
            // Navigate back to HomeView
        }, trailing: Button("Share") {
        })
    }
}

}

struct ExpenseLoggingView_Previews: PreviewProvider { static var previews: some View { ExpenseLoggingView(group: Group(id: UUID(), name: "Sample Group", members: [User(id: UUID(), name: "Alice", email: "alice@example.com"), User(id: UUID(), name: "Bob", email: "bob@example.com")], expenses: [])) } }

I would appreciate any help! :)

1      

Can we see the User, Group and Expense set up

When I comment out the PAYER Section then there are no errors. I cannot see what group.friends is!

struct ContentView: View {
    @State private var date: Date = .now // <- use Date.now
    @State private var description: String = ""
    @State private var amount: Double = 0.0
    @State private var payer: User?
    @ObservedObject var group: Group

    var body: some View {
        NavigationView {
                Form {
                    Section(header: Text("Expense Details")) {
                        DatePicker("Date", selection: $date, displayedComponents: .date)

                        TextField("Description", text: $description)

                        TextField("Amount", value: $amount, formatter: NumberFormatter())
                    }
// Commented out NO ERRORS

//                    Section(header: Text("Payer")) {
//                        // Payer selection
//                        Picker("Payer", selection: $payer) {
//                            ForEach(group.friends) { friend in
//                                Text(friend.name).tag(friend as User?)
//                            }
//                        }
//                    }

                    Section {
                        Button("Submit Expense") {
                            if let payer = payer {
                                let expense = Expense(id: UUID(), date: date, description: description, amount: amount, payer: payer)
                                group.expenses.append(expense)
                            }
                        }
                    }
                }
                .navigationBarTitle("Add Expense")
                .toolbar {                                                                                // <- Use .toolbar
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button("Home") { }
                    }

                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("Share") { }
                    }
                }
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(group: .example)
    }
}

struct User {
    let id: UUID
    let name: String
    let email: String
}

class Group: ObservableObject {
    let id: UUID
    let name: String
    let members: [User]
    var expenses: [Expense]

    init(id: UUID, name: String, members: [User], expenses: [Expense]) {
        self.id = id
        self.name = name
        self.members = members
        self.expenses = expenses
    }

// Set up example data for previews
    static let example = Group(id: UUID(), name: "Sample Group", members: [
        User(id: UUID(), name: "Alice", email: "alice@example.com"),
        User(id: UUID(), name: "Bob", email: "bob@example.com")], expenses: [])
}

struct Expense {
    let id: UUID
    let date: Date
    let description: String
    let amount: Double
    let payer: User
}

1      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.