The NumberFormatter seems to stop working with Decimal type properties since Xcode version 13 beta 3 (13A5192j).
The following code works up to Xcode version 13 beta 2 and then no longer.
Question:
What must be changed in the code so that entered numerical values are stored in the Decimal property again?
Code Example
import SwiftUI
struct Model {
var decimalQuantity: Decimal = 2.43
var someText: String = ""
}
struct DecimalBindingView: View {
@Binding var model: Model
private let numberFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.generatesDecimalNumbers = true
return formatter
}()
var body: some View {
Form {
Section {
Label {
Text("This text field does not work correctly. \nIt doesn't write the value back to the property when the field loses focus or you click Enter.")
} icon: {
Image(systemName: "xmark.circle").foregroundColor(Color.red).scaleEffect(1.4)
}
.lineLimit(10)
// The formatter or the two-way data binding does not seem to work.
// The new value is not written back to the property correctly.
// Question: What needs to be changed to make this work?
TextField(
"Input a number of type decimal…",
value: $model.decimalQuantity,
formatter: numberFormatter)
}
Section {
TextField(
"Sample field, so you can leave the other field",
text: $model.someText)
}
}
}
}
Minimal Xcode Project
https://github.com/brads-dev-dojo/SwiftUITextFieldWithDecimalValueTypeApp