Hi new to coding

having an issue with this and cannot figure out what is wrong if any bright sparks can help would be most great full.
Using Swiftui

var body: some View {. Type '()' cannot conform to 'View'

    Group{
        
     
            
            Text("Calculator")
                .font(.body)
            
            Spacer()
            
            Text("Area")
         
            
                    TextField("L", text: $L).keyboardType(.numberPad)
                    TextField("w", text:$W).keyboardType(.numberPad)
                    TextField("D", text:$D).keyboardType(.numberPad)
                    
                    Text("M2 \(answer)")
                }
                
                
                
                Button(answer, action: {
                    let convertInput = Int(L) ?? 0
                    W * D})
                answer = String(convertInput)
            }
            { Text("Calculate")
                
                
                
                
                Menu{
                    Button(action: {
                        choicemade = "MOT Type 1"
                    }, label: { Text("MOT Type 1")})
                    
                    Button(action: {
                        choicemade = "Crushed Concrete"
                    }, label: { Text("Crushed Concrete")})
                    
                    Button(action: {
                        choicemade = "Sand"
                    }, label: { Text("Sand")})
                }
                
                
                
                
                
            label: {
                Label(
                    title: {Text("\(choicemade)") },
                    icon: {Image(systemName: "Plus")}
                )
            }
                
                
            }
            
            
            if (choicemade == "MOT Type 1"){
                Text("MOT Type 1 selected")
            }
            if (choicemade == "Crushed Concrete"){
                Text("Crushed Concrete selected")
            }
            if (choicemade == "Sand"){
                Text("Sand selected")
            }
            Spacer()
            Spacer()
            
            
        }
answer = String(convertInput)

You want to move that into the action closure:

                Button(answer, action: {
                    let convertInput = Int(L) ?? 0
                    W * D
                    answer = String(convertInput)
})
                

Genuis Thank you

\
Button(answer, action: {
let convertInput = Int(L) ?? 0
W * D
answer = String(convertInput)
}
{ Text("Calculate"). X Extra trailing closure passed in call

It seems you have a lot of debris leftover from you stumbling around trying to find a solution. I removed it, and put your code through an autoformatter tool :)

Group {
    Text("Calculator").font(.body)
    Spacer()
    Text("Area")
    TextField("L", text: $L).keyboardType(.numberPad)
    TextField("w", text: $W).keyboardType(.numberPad)
    TextField("D", text: $D).keyboardType(.numberPad)
    Text("M2 \(answer)")
    // } random bracket

    // Button(answer, action: { extra parameter at the start, assuming you want to create the button the same way as the others
    Button(
        action: {
            let convertInput = Int(L) ?? 0
            // W * D random multiplication
            answer = String(convertInput)
        }, 
        label: { Text("Calculate") }
    )
    Menu {
        Button(action: { choicemade = "MOT Type 1" }, label: { Text("MOT Type 1") })
        Button(action: { choicemade = "Crushed Concrete" }, label: { Text("Crushed Concrete") })
        Button(action: { choicemade = "Sand" }, label: { Text("Sand") })
    }
    // label: { random label
    Label(title: { Text("\(choicemade)") }, icon: { Image(systemName: "Plus") })
    // } two random closing brackets

    // }

    if choicemade == "MOT Type 1" { 
        Text("MOT Type 1 selected") 
    }
    if choicemade == "Crushed Concrete" { 
        Text("Crushed Concrete selected") 
    }
    if choicemade == "Sand" { 
        Text("Sand selected")
    }
    Spacer()
    Spacer()
}

thanks after doing this I'm having trouble getting this to perform

the idea is that L = length W = width and D = Depth
are multiplied together when the button is clicked.

it now throws Binary operator '*' cannot be applied to two 'String' operands

What do you want to happen when someone types "123hahahahaha" into the "L" text field? (for example by connecting a bluetooth physical keyboard)

  1. I promise nobody will do that, even by mistake, and even if someone does it I don't care what happens
  2. if someone types in something that is not a number, treat it like some default number, for example zero
  3. show error message
  4. something else?

ok will work on that is there a better way of writing

the button to calculate the area

The error is telling you that you cannot multiply strings because what would be the result of "elephant" * "xzcviuh" ?

You have to either not use strings at all (which means you cannot use textfields), or somehow handle the error in conversion.

You didn't choose any of the options I gave you, so I'll choose for you: 3. show error message

Group {
    Text("Calculator").font(.body)
    Spacer()
    Text("Area")
    TextField("L", text: $L).keyboardType(.numberPad)
    TextField("w", text: $W).keyboardType(.numberPad)
    TextField("D", text: $D).keyboardType(.numberPad)
    Text("M2 \(answer)")

    if let length = Int(L), let width = Int(W), let height = Int(H) {
        Button(
            action: {
                answer = String(length * width * height)
            }, 
            label: { Text("Calculate") }
        )
    } else {
        Text("You wrote something that is not a number :(")
    }

    Menu {
        Button(action: { choicemade = "MOT Type 1" }, label: { Text("MOT Type 1") })
        Button(action: { choicemade = "Crushed Concrete" }, label: { Text("Crushed Concrete") })
        Button(action: { choicemade = "Sand" }, label: { Text("Sand") })
    }

    Label(title: { Text("\(choicemade)") }, icon: { Image(systemName: "Plus") })

    if choicemade == "MOT Type 1" { 
        Text("MOT Type 1 selected") 
    }
    if choicemade == "Crushed Concrete" { 
        Text("Crushed Concrete selected") 
    }
    if choicemade == "Sand" { 
        Text("Sand selected")
    }
    Spacer()
    Spacer()
}
1 Like

ah I get it. Thank you so much only started to learn coding a month ago this is really help full. again many thanks.

1 Like