mooncar522
mooncar522

Reputation: 11

SwiftUI: Generic parameter 'C0' could not be inferred

I've been working with this code and I keep getting this error: ''Generic parameter 'C0' could not be inferred'' Additionally it says 'In call to function 'buildBlock' (SwiftUI.ViewBuilder)'on my HStack when I include this line of code:

self.userData.tempBatchUnit = productName

I am not sure why. The code works fine without that line of code. Many thanks

struct enterProductUnitView: View {
    @EnvironmentObject var userData: UserData
    @State var productName: String = ""

    var body: some View {
        VStack {
            HStack { // error Generic parameter 'C0' could not be inferred
                Text("Product Unit:")
                    .font(.headline)
                Spacer()
                NavigationLink(destination: InstructionsView(desireInstructions: "Product Unit")) {
                    Text("?")
                }
            }

             Text("ex: bags of popcorn, jars of jam etc.")
                .font(.subheadline)
            TextField("Enter here", text: $productName)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .padding()
                .padding(.leading)
            self.userData.tempBatchUnit = productName
        }
    }
}

Upvotes: 1

Views: 1229

Answers (1)

Asperi
Asperi

Reputation: 257693

Remove the following line - it is not allowed in bodyview builder

self.userData.tempBatchUnit = productName

I assume it should be in .onCommit

    TextField("Enter here", text: $productName, onCommit: {
            self.userData.tempBatchUnit = self.productName
        })
        .textFieldStyle(RoundedBorderTextFieldStyle())
        .padding()
        .padding(.leading)

Upvotes: 3

Related Questions