如何在SwiftUI中使用扩展从Binding转换为Binding

huangapple go评论80阅读模式
英文:

How to bridge from Binding<Float> to Binding<String> using an Extension in SwiftUI?

问题

以下是您要翻译的内容:

Context

我有一个将Binding限制为FloatExtension,将其转换为String以在SwiftUITextField中使用。然而,在无法确定导致问题的原因时,我遇到了两种奇怪的行为:

  1. 当输入超过4位数时,例如12345,TextField会清空。

  2. 当输入小数分隔符(例如美国的".")时,TextField会清空。

Code

public extension Binding where Value == Float {
    var adapter: Binding<String> {
        Binding<String>(
            get: {
                guard self.wrappedValue != -1 else { return "" }
                
                let formatter = NumberFormatter()
                formatter.locale = Locale.current
                formatter.numberStyle = .decimal
                
                return formatter.string(from: NSNumber(value: self.wrappedValue)) ?? ""
            },
            set: {
                let formatter = NumberFormatter()
                formatter.locale = Locale.current
                formatter.numberStyle = .decimal
                
                self.wrappedValue = formatter.number(from: $0)?.floatValue ?? -1
            }
        )
    }
}

如果您需要进一步的帮助,请随时提问。

英文:

Context

I have an Extension of Binding limited to Float which converts it into a String for usage with the SwiftUI TextField. However, I encountered two odd behaviours while not being able to identify the causing issues:

  1. When entering more than 4 digits, e.g. 12345, the TextField empties

  2. When entering a decimal separator (e.g. "." in the US), the TextField empties


Code

public extension Binding where Value == Float {
    var adapter: Binding&lt;String&gt; {
        Binding&lt;String&gt;(
            get: {
                guard self.wrappedValue != -1 else { return &quot;&quot; }
                
                let formatter = NumberFormatter()
                formatter.locale = Locale.current
                formatter.numberStyle = .decimal
                
                return formatter.string(from: NSNumber(value: self.wrappedValue)) ?? &quot;&quot;
            },
            set: {
                let formatter = NumberFormatter()
                formatter.locale = Locale.current
                formatter.numberStyle = .decimal
                
                self.wrappedValue = formatter.number(from: $0)?.floatValue ?? -1
            }
        )
    }
}

Question

  • What causes these errors and how can I achieve my goal of bridging from Binding&lt;Float&gt; to Binding&lt;String&gt;?

答案1

得分: 1

不要重新发明轮子,而是使用新的格式化样式

@State private var value: Float = 0

var body: some View {
//...
TextField("值", value: $value, format: .number)
//...
}

英文:

Don't reinvent the wheel, instead use the new formatting style

@State private var value: Float = 0

var body: some View {
    //...
    TextField(&quot;Value&quot;, value: $value, format: .number)
    //...
}

huangapple
  • 本文由 发表于 2023年5月28日 21:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定