Hi @Claude31
The issue is not with the Text() & specifier it is with the TextField()
TextField("1.234", value: $dividend.value, formatter: decimalFormatter)
where the decimal formatter seems to not work.
Changing to
TextField("1.234", value: $dividend.value, format: .number)
seems to have fixed the problem.
I made a sample project which I submitted to Apple Feedback (FB9963196)
import SwiftUI
class Data: ObservableObject {
@Published var id = UUID().uuidString
@Published var value1: Double?
@Published var value2: Int?
}
struct ContentView: View {
@ObservedObject var data = Data()
var numberFormatter: NumberFormatter = {
let nf = NumberFormatter()
nf.locale = Locale.current
nf.numberStyle = .decimal
return nf
}()
var decimalFormatter: NumberFormatter = {
let df = NumberFormatter()
df.locale = Locale.current
df.numberStyle = .decimal
df.generatesDecimalNumbers = true
return df
}()
var body: some View {
let calc = data.value1 ?? 0 * Double(data.value2 ?? 0)
Form {
HStack {
Text("Value 1")
TextField("1.234", value: $data.value1, formatter: decimalFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
}
HStack {
Text("Value 2")
TextField("10", value: $data.value2, formatter: numberFormatter)
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
}
Section {
Text("Calculated value = \(calc)")
}
}
}
}
if you run this in the simulator in iOS15.2 and 15.4 you will see the issue with the TextField.
As Sam NZ above has indicated that this is due to the value being optional in his project.
Topic:
Programming Languages
SubTopic:
Swift
Tags: