Create a computed @State variable in SwiftUI

Here is an approach I prefer with computed property & binding “on-the-fly”

private var bindableIsVisibleError: Binding<Bool> { Binding (
    get: { self.model.usernameResult.isVisibleError },
    set: { if !$0 { self.model.dismissUsernameResultError() } }
    )
}

and usage (as specified)

Toggle(isOn: bindableIsVisibleError, label: { EmptyView() })

Leave a Comment

tech