I have the following view, which works fine:
struct Decimal_text_field_nullable : View {
@State var name : String
@State var place_holder : String
@Binding var value : Double?
var format_as_decimal: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter
}
var body: some View {
VStack(alignment: .leading) {
Text(name)
.font(.headline)
TextField( place_holder, value : $value, formatter: format_as_decimal )
.add_close_button()
.keyboardType(.decimalPad)
.padding(.all)
.background(Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 0.2))
}
.padding(.horizontal, 15)
}
however I have to have a Decimal_text_field_nullable and a Decimal_text_field with exactly the same code except for a question mark on the definition of value - because I can't bind a Double value to a Double? value.
Is there any way to make a decimal_text_field where this can work:
VStack()
{
Decimal_text_field("non optional value", value = $non_nullable_double )
Decimal_text_field("optional_value", value = $nullable_double )
}