JonoDK22
(Jon)
1
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()
}
cukr
2
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)
})
JonoDK22
(Jon)
3
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
cukr
4
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()
}
JonoDK22
(Jon)
5
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
cukr
6
What do you want to happen when someone types "123hahahahaha" into the "L" text field? (for example by connecting a bluetooth physical keyboard)
- I promise nobody will do that, even by mistake, and even if someone does it I don't care what happens
- if someone types in something that is not a number, treat it like some default number, for example zero
- show error message
- something else?
JonoDK22
(Jon)
7
ok will work on that is there a better way of writing
the button to calculate the area
cukr
8
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
JonoDK22
(Jon)
9
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