SwiftUI Custom Bindings

In swiftui you can create custom bindings

https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-custom-bindings

struct ContentView: View {
    @State private var username = ""

    var body: some View {
        let binding = Binding(
            get: { self.username },
            set: { self.username = $0 }
        )

        return VStack {
            TextField("Enter your name", text: binding)
        }
    }
}

This may seem obvious, but when I was learning this wasn't clear to me at all. Learning this (and about property-wrappers in general) helped open my eyes to how data is handled in SwiftUI and enabled me to build more complex and composable views.

Last Updated January 04, 2021