看起來 Xcode 13.3 用格式化程式破壞了 TextField。在下面的示例中,Text 應該顯示在 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
uj5u.com熱心網友回復:
找到了一個不同的 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)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/449112.html
