【发布时间】:2019-07-30 19:46:45
【问题描述】:
我正在尝试实现在编辑模式详细视图中更改货币值的功能,但我正在努力获取格式化输入以使数值正常工作。
struct JobDetailView_Edit: View {
@State var jobDetails: JobDetails
var currencyFormatter: NumberFormatter = {
let f = NumberFormatter()
f.numberStyle = .currency
return f
}()
var body: some View {
Form {
Section(header: Text("General")) {
HStack {
Text("Job Name")
Spacer()
TextField($jobDetails.jobName)
.multilineTextAlignment(.trailing)
}
TextField($jobDetails.hourlyRateBasic, formatter: currencyFormatter)
}
... other unimportant code...
数据正确传入,文本字段显示存储在 $jobDetails.hourlyRateBasic 中的格式化值,但是,如果它是字符串,我无法编辑该字段。如果我尝试编辑该字段,然后在模拟器中按 Enter,我会收到以下错误消息:
[SwiftUI] Failure setting binding's value. The supplied formatter does not produce values of type Double. This may be resolved by ensuring the binding and the output of the formatter are of the same type.
仅供参考 $jobDetails.hourlyRateBasic 是 double 类型。
【问题讨论】: