0

I have a simple model class I want to use with SwiftData:

@Model
class Destination {
    var name: String
    var expenses: Double
    
    init(name: String = "", expenses: Double = 0.0) {
        self.name = name
        self.expenses = expenses
    }
}

I want to edit entries that are created, and for this I have created the following EditView, where I use @Bindable:

struct EditDestinationView: View {
    
    @Bindable var destination: Destination
    
    var body: some View {
        Form {
            TextField("Name", text: $destination.name)
            TextField("Expenses", text: $destination.expenses)
            }
        }
        .navigationTitle("Edit Destination")
        .navigationBarTitleDisplayMode(.inline)
    }
}

The problem I have is the double value "expenses", which I can't use in TextField.

Cannot convert value of type 'Binding<Double>' to expected argument type 'Binding<String>'

Question: What would be the best way to read/update destination.expenses with the value from the textField?

2
  • The question is not particularly related to SwiftData. There are TextField initializers with value and format. Commented Oct 18, 2023 at 7:07
  • Ah, you're absolutely right ... I was thinking too complicated in terms of SwiftData and @Binding :-( Thx a lot for your hint vadian. Commented Oct 18, 2023 at 7:12

1 Answer 1

1

As vadian mentioned, it's just simple:

TextField("Expenses", value: $destination.expenses, format: .number)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.