NEW BOOK! SwiftUI Fundamentals: The essential guide to SwiftUI core concepts and APIs. Learn more ...NEW BOOK! SwiftUI Fundamentals:Master SwiftUI core concepts and APIs. Learn more...
Quick Tip Icon
Quick Tip

Preview SwiftUI views with bindings using @Previewable

Xcode 16 introduced the Previewable macro, making it easier to preview SwiftUI views with bindings. By annotating dynamic properties like @State in a #Preview body with @Previewable, we can pass them as bindings to views directly.

Here is an example on how we can easily setup a fully interactive preview for a basic counter view that accepts a binding:

struct CounterView: View {
    @Binding var count: Int
    
    var body: some View {
        VStack {
            Text("Count: \(count)")
            Button("Increment count") {
                count += 1
            }
        }
    }
}

#Preview {
    @Previewable @State var count = 0
    CounterView(count: $count)
}

This example creates an interactive preview where the count value can be updated in real-time using the provided button.

Xcode preview showing count 3 and increment button Xcode preview showing count 3 and increment button

@Previewable is supported on iOS 17, macOS 14, and other platforms.

SwiftUI Fundamentals by Natalia Panferova book coverSwiftUI Fundamentals by Natalia Panferova book cover

Deepen your understanding of SwiftUI!$35

The essential guide to SwiftUI core concepts and APIs

SwiftUI Fundamentalsby Natalia Panferova

  • Explore the key APIs and design patterns that form the foundation of SwiftUI
  • Develop a deep, practical understanding of how SwiftUI works under the hood
  • Learn from a former Apple engineer who worked on widely used SwiftUI APIs

Deepen your understanding of SwiftUI!

The essential guide to SwiftUI core concepts and APIs

SwiftUI Fundamentals by Natalia Panferova book coverSwiftUI Fundamentals by Natalia Panferova book cover

SwiftUI Fundamentals

by Natalia Panferova

$35