【问题标题】:CoreData SwiftUI TextField - Change Binding datatype to Double didn't workCoreData SwiftUI TextField - 将绑定数据类型更改为 Double 不起作用
【发布时间】:2020-04-12 12:49:09
【问题描述】:

为什么不能将数据类型更改为Binding<Double>? 在 CoreData 定义中,变量定义为 Double。 如果我对 Binding<String> 和 String 类型的另一个 CoreData 变量尝试相同的操作,我不会收到任何错误,并且一切正常。 Xcode 错误:“无法在当前上下文中推断闭包类型”

TextField("motoric1points", text: Binding<Double>(get: {player.motoric1points ?? "<none>"}, set: {player.motoric1points = $0}))

一些进一步的信息: 我需要这种形式的 TextField,因为我在使用循环创建的 GridStack 中使用它。对我来说非常重要的是,我可以“实时”编辑变量。

我很高兴得到任何帮助。提前谢谢了! :)

【问题讨论】:

    标签: swift core-data swiftui swift5.1


    【解决方案1】:

    我认为你想要反过来:

    TextField("motoric1points", text: Binding<String>(
        get: {
            if let motoric1points = player.motoric1points {
                return String(motoric1points)
            } else {
                return "<none>"
            }
        },
        set: {
            player.motoric1points = Double($0)
        }
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多