使用 Xcode 13.3 构建时格式化程序损坏的 SwiftUI TextField?
SwiftUI TextField with formatter broken when built with Xcode 13.3?
看起来 Xcode 13.3 使用格式化程序破坏了 TextField。在下面的示例中,文本应显示在 TextField 中输入的值,在使用 Xcode 13.2.1(再次降级以测试)构建时可以正常工作,但使用 Xcode 13.3 TextField 不会更新其绑定值.
struct ContentView: View {
@State var value: Float?
let decimalFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 3
return formatter
}()
var body: some View {
VStack {
TextField("some float", value: $value, formatter: decimalFormatter)
.multilineTextAlignment(.center)
.keyboardType(.decimalPad)
Text("Value: \(value != nil ? String(value!) : "nil")")
}
}
}
- 我的代码是否存在某种缺陷,因为它停止使用 Xcode 13.3,或者这是 Xcode 13.3 的错误?
- 知道如何修复我的代码或解决 Xcode 13.3 中的错误吗? - 我目前的解决方法是降级到 Xcode 13.2.1
找到了一个不同的 API,它可以满足我对可选选项的预期:https://developer.apple.com/documentation/swiftui/textfield/init(_:value:format:prompt:)-6ug7k
TextField("some float", value: $value, format: FloatingPointFormatStyle.number)
虽然这不能解释为什么它以前使用 Formatter 而停止使用 Xcode 13.3,但至少这个 API 似乎是特定于可选值的。
将小数位数限制为 3 也有效,只是不会在编辑期间立即应用,而是在焦点更改之后应用。
TextField("some float", value: $value, format: FloatingPointFormatStyle.number
.precision(NumberFormatStyleConfiguration.Precision.fractionLength(0...3)))
使用 中的方法更正了代码示例。这仅适用于 iOS 15+.
TextField("some float",
value: $value,
format: .currency(code: "USD"))
.onChange(of: value) { newValue in
print ("value is \(newValue)")
}
又一个例子format
。这里使用 Apple docs on FloatingPointFormatStyle:
中的一种实例方法
TextField("some float",
value: $value,
format: FloatingPointFormatStyle().decimalSeparator(strategy: .automatic))
有关此方法的更多信息在 Apple docs for TextField.init(_:value:format:prompt:)
看起来 Xcode 13.3 使用格式化程序破坏了 TextField。在下面的示例中,文本应显示在 TextField 中输入的值,在使用 Xcode 13.2.1(再次降级以测试)构建时可以正常工作,但使用 Xcode 13.3 TextField 不会更新其绑定值.
struct ContentView: View {
@State var value: Float?
let decimalFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 3
return formatter
}()
var body: some View {
VStack {
TextField("some float", value: $value, formatter: decimalFormatter)
.multilineTextAlignment(.center)
.keyboardType(.decimalPad)
Text("Value: \(value != nil ? String(value!) : "nil")")
}
}
}
- 我的代码是否存在某种缺陷,因为它停止使用 Xcode 13.3,或者这是 Xcode 13.3 的错误?
- 知道如何修复我的代码或解决 Xcode 13.3 中的错误吗? - 我目前的解决方法是降级到 Xcode 13.2.1
找到了一个不同的 API,它可以满足我对可选选项的预期:https://developer.apple.com/documentation/swiftui/textfield/init(_:value:format:prompt:)-6ug7k
TextField("some float", value: $value, format: FloatingPointFormatStyle.number)
虽然这不能解释为什么它以前使用 Formatter 而停止使用 Xcode 13.3,但至少这个 API 似乎是特定于可选值的。
将小数位数限制为 3 也有效,只是不会在编辑期间立即应用,而是在焦点更改之后应用。
TextField("some float", value: $value, format: FloatingPointFormatStyle.number
.precision(NumberFormatStyleConfiguration.Precision.fractionLength(0...3)))
使用
TextField("some float",
value: $value,
format: .currency(code: "USD"))
.onChange(of: value) { newValue in
print ("value is \(newValue)")
}
又一个例子format
。这里使用 Apple docs on FloatingPointFormatStyle:
TextField("some float",
value: $value,
format: FloatingPointFormatStyle().decimalSeparator(strategy: .automatic))
有关此方法的更多信息在 Apple docs for TextField.init(_:value:format:prompt:)