Hello
How di i fix the error at line 16: I
nstance member 'deci' cannot be used on type 'BMIView'; did you mean to use a value of this type instead?
Thank you
How di i fix the error at line 16: I
nstance member 'deci' cannot be used on type 'BMIView'; did you mean to use a value of this type instead?
Code Block import SwiftUI struct BMIView: View { @State var deci = "3" var numberFormatter: NumberFormatter = { let nf = NumberFormatter() nf.locale = Locale.current nf.numberStyle = .decimal nf.maximumFractionDigits = Int(deci) ?? 0 return nf }() @State private var height = "" @State private var weight = "" @Environment(\.presentationMode) var presentationMode var inputAfterConvertions: Double { //Use NumberFormatter to read numeric values let hh = numberFormatter.number(from: height)?.doubleValue ?? 0 //<- let ww = numberFormatter.number(from: weight)?.doubleValue ?? 0 //<- var ris: Double = 0 if hh > 0 && ww > 0{ ris = (ww / (hh * hh)) * 10000 return ris } return 0 } var body: some View { NavigationView{ Form{ Section(header: Text("Enter your height in cm")){ TextField("Input",text: $height) .keyboardType(.decimalPad) } Section(header: Text("Enter your Weight in kg")){ TextField("Input",text: $weight) .keyboardType(.decimalPad) } Section(header: Text("Check result")){ Text("\(inputAfterConvertions as NSNumber, formatter: numberFormatter)") //<- } } .navigationTitle("BMI") } } }
Thank you
Don't do it here, but later when you use numberFormatter:
Code Block @State var deci = "3" var numberFormatter: NumberFormatter = { let nf = NumberFormatter() nf.locale = Locale.current nf.numberStyle = .decimal return nf }() @State private var height = "" @State private var weight = "" @Environment(\.presentationMode) var presentationMode var inputAfterConvertions: Double { //Use NumberFormatter to read numeric values numberFormatter.maximumFractionDigits = Int(deci) ?? 0 let hh = numberFormatter.number(from: height)?.doubleValue ?? 0 //<- let ww = numberFormatter.number(from: weight)?.doubleValue ?? 0 //<- var ris: Double = 0 if hh > 0 && ww > 0{ ris = (ww / (hh * hh)) * 10000 return ris } return 0 }