SwiftyJanek
SwiftyJanek

Reputation: 1

CoreData SwiftUI TextField - Change Binding datatype to Double didn't work

why isn't it possible to change the datatype to Binding<Double>? In the CoreData definition the variable is defined as Double. If I try the same with Binding<String> and another CoreData variable from type String I don't get any errors and everything works fine. Xcode error: "Unable to infer closure type in the current context"

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

Some further information: I need the TextField in a form like this because I'm using it in a GridStack that I created with loops. It is very important for me, that I can edit the variables in "realtime".

I'm glad about any help I get. Many thanks in advance! :)

Upvotes: 0

Views: 256

Answers (1)

Yonat
Yonat

Reputation: 4608

I think you want it the other way around:

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

Upvotes: 1

Related Questions