Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created November 10, 2022 19:09
Show Gist options
  • Save hmlongco/8efe9c66ab8b38056e67a802881c491a to your computer and use it in GitHub Desktop.
Save hmlongco/8efe9c66ab8b38056e67a802881c491a to your computer and use it in GitHub Desktop.
Binding+variable
extension Binding {
public static func variable(_ value: Value) -> Binding<Value> {
var state = value
return Binding<Value> {
state
} set: {
state = $0
}
}
}
@hmlongco
Copy link
Author

struct TestToggle: View {
    @Binding var value: Bool
    var body: some View {
        Toggle(isOn: $value) {
            Text("Toggle Me")
        }
        .padding()
    }
}

struct TestToggle_Previews: PreviewProvider {
    static var previews: some View {
        TestToggle(value: .variable(true))
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment