I have to do multiple calculations using one of the two options of units using picker in swiftUI. I am unable to complete the calculations, The code for one variable is below
struct ContentView: View {
@State var height = ""
@State var heightCms: Double = 0.0
var heightOptions = ["cms", "inches"]
@State private var heightselection = "cms"
var body: some View {
VStack {
HStack {
Text("Height:")
.font(.title)
.foregroundColor(.black)
Spacer()
TextField("Height", text: $height)
.frame(width: 150, height: 50)
Picker("Select Height Units: ", selection: $heightselection, content: {
ForEach(heightOptions, id: \.self) {
Text($0)
}
})
Spacer()
}
Text("You selected: \(heightselection)")
Text("Height in Cms: \(heightCms)")
}.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension ContentView {
func calculate() {
heightCms = heightselection == "cms" ? String(Double(height)) : (String(Double(height)) * 2.54)
}
}
Would appreciate help in completing the task and understanding the error. The code provide correct selection answer but when try to apply in calculation then it doesn't work