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

I am getting the error in the title on the following form. Cannot figure out why. Please help! I do not have any custom style for forms or anything. Also getting an info bubble on the line as well that says "'init(_:)' declared here (SwiftUI.Form)"

Form { Section(header: Text("Employee Name")) { TextField("First Name", text: $user.user?.firstName ?? "") TextField("Last Name", text: $user.user?.lastName) }

            Section(header: Text("Employee and Store Info")) {
                TextField("Store Number", text: Int($user.user?.storeNumber))
                    .keyboardType(.numberPad)
                TextField("Team Member Number", text: Int($user.user?.teamMemberNumber))
            }
        }
Answered by ssmith_c in 751380022

what code do you have a problem with? The unformatted code, or the code in the code block?

The unformatted code works for me, but I had to replace

$user.user?.firstName ?? ""

with $firstName, where I declared a

@State private var firstName = "Joe",

and similarly for lastName.

The formatted code shouldn't compile, because Int() returns an Int, while ``TextField's text: parameter should be a Binding<String> Try simplifying your code to something you can post as a complete sample. That alone may lead you to a solution. If it doesn't, post the complete sample here and indicate clearly what is not working (you said "in the title on the following form", but there is no "title" field). Also mention what platform (macOS, iOS etc) you are targeting, and what Xcode version you are using.

Accepted Answer

what code do you have a problem with? The unformatted code, or the code in the code block?

The unformatted code works for me, but I had to replace

$user.user?.firstName ?? ""

with $firstName, where I declared a

@State private var firstName = "Joe",

and similarly for lastName.

The formatted code shouldn't compile, because Int() returns an Int, while ``TextField's text: parameter should be a Binding<String> Try simplifying your code to something you can post as a complete sample. That alone may lead you to a solution. If it doesn't, post the complete sample here and indicate clearly what is not working (you said "in the title on the following form", but there is no "title" field). Also mention what platform (macOS, iOS etc) you are targeting, and what Xcode version you are using.

Will adding the @State private var firstName = "Joe" still assign them to the codable variables?

the problem appears to lie with the second parameter you pass to the TextField view builder. You must have a var user somewhere - what kind of object is user, and does it really have a property called user inside it?

I'm using the @State wrapper because it is convenient for state variables which outlive the ContentView itself, but it often isn't useful in a real application. An instance of your User struct will normally live in your data model.

I can get your code to compile if I write the following. Note there is more than one way to put an Int into a TextField.

struct User: Codable {
    var firstName: String
    var lastName: String
    var isAdmin: Bool
    var storeNumber: Int
    var teamMemberNumber: Int
    var userApproved: Bool
    var userCreated: Bool
    
}

struct ContentView:  View {
    
    @State private var user = User(firstName: "first", lastName: "lastName", isAdmin: false, storeNumber: 24, teamMemberNumber: 42, userApproved: false, userCreated: false)
    
    var body: some View {
        
        Form {
            Section(header: Text("Employee Name")) {
                Text(user.firstName)
                TextField("First Name", text: $user.firstName)
                TextField("Last Name", text: $user.lastName)
                
            }
            Section(header: Text("Employee and Store Info")) {
                TextField("Store Number", value: $user.storeNumber, formatter: NumberFormatter())
                    .keyboardType(.numberPad)
                TextField("Team Member Number", value: $user.teamMemberNumber, formatter: NumberFormatter())
            }
        }
    }
}

and I can reproduce your error by replacing the last line with this one

TextField("Team Member Number", text: Int($user.user?.teamMemberNumber))

the TextField expect to the text: parameter to be a binding to String, not an Int. Instead of complaining about that, the compiler complains about the Form declaration. The usual initializer for Form is this one

public init(@ViewBuilder content: () -> Content)

while an extension on Form defines this one

public init(_ configuration: FormStyleConfiguration)

The parameter to init which you write after Form might be a View builder, and might be a FormStyleConfiguration. If the expression is malformed, the compiler has to make a guess about what you meant to write, here it guessed wrong, gives you a misleading error message and doesn't point out the real source of the error (the declaration of TextField a text: parameter which is mismatched).

The compiler does keep getting better at pointing out errors. If you file a bug on this Apple might fix it.

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