Skip to content

Instantly share code, notes, and snippets.

Last active June 11, 2023 02:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A wrapper to create any number of arbitrary state variables using parameter packs for testing bindable values in a preview.
import SwiftUI
struct PreviewState<each T, Content: View>: View {
@State private var value: (repeat each T)
var content: (Binding<(repeat each T)>) -> Content
_ value: repeat each T,
@ViewBuilder content: @escaping (Binding<(repeat each T)>) -> Content
) {
self._value = State(initialValue: (repeat each value))
self.content = content
var body: some View {
#Preview {
VStack {
PreviewState(false) {
Toggle("First Value", isOn: $0)
PreviewState(false, 1, 10.0) {
Toggle("First Value", isOn: $0.0)
Stepper("Second Value", value: $0.1, in: 0...5)
Slider(value: $0.2, in: 0...100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment