SwiftUI: How to implement a custom init with @Binding variables

I’m working on a money input screen and need to implement a custom init to set a state variable based on the initialized amount.

I thought this would work, but I’m getting a compiler error of:

Cannot assign value of type 'Binding<Double>' to type 'Double'

struct AmountView : View {
    @Binding var amount: Double

    @State var includeDecimal = false

    init(amount: Binding<Double>) {
        self.amount = amount
        self.includeDecimal = round(amount)-amount > 0
    }
    ...
}

7 Answers
7

Leave a Comment