A property wrapper that can be used to abstract the state and binding properties.
struct Counter: View {
@AbstractState var count: Int
init(initialValue: Int) {
_count = AbstractState(initialValue)
}
init(binding: Binding<Int>) {
_count = AbstractState(binding)
}
var body: some View {
Button("Up \(count)") {
count += 1
}
}
}
#Preview("State") {
Counter(initialValue: 1)
}
struct Wrapper: View {
@State private var count: Int = 1
var body: some View {
Counter(binding: $count)
}
}
#Preview("Binding") {
Wrapper()
}