Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 662 Bytes

README.md

File metadata and controls

47 lines (34 loc) · 662 Bytes

SwiftUI - AbstractState

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()
}