Created
June 28, 2023 06:41
-
-
Save klundberg/a68b47f7c16f0d4770ad62034397c513 to your computer and use it in GitHub Desktop.
A mutable variant of Binding.constant to help with swiftui previews
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Binding { | |
/// like Binding.constant but lets you update the value and stores that updated value via a captured enclosing reference. | |
public static func initially(_ value: Value) -> Binding<Value> { | |
let box = Box(value) | |
return .init( | |
get: { box.value }, | |
set: { box.value = $0 } | |
) | |
} | |
private class Box<Value> { | |
var value: Value | |
init(_ value: Value) { self.value = value } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment