How to preview SwiftData in XCode Previews

A SwiftUI guide for previewing SwiftData actions and data in XCode Previews.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 08/20/2024 at 14:36
Last Updated on 08/30/2024 at 13:18
<p>Three flowers that represents SwiftUI, SwiftData and XCode side by side with the text “Preview SwiftData” beneath it.</p>

A SwiftUI guide for previewing SwiftData actions and data in XCode Previews.

Subscribe

The code for the demonstrations detailed on this post can be found at the tutorials/swiftdata/101 branch within the repository below.

View Repository

If you wish to learn more about the code that we provided for these demonstrations, consult the guide below.

SwiftUI SwiftData Quick Start Guide

Preview Actions

<p>A screenshot of the code for the sample screen highlighting how we previewed the SwiftData by passing a .modelContainer to the preview.</p>

To preview actions such as inserting or deleting items, add a .modelContainer(for: SWIFT_MODEL_NAME.self) to your XCode Preview.

Preview Data

<p>A screenshot of the code for the SwiftDataListRow view. Highlighted is how we created a mock SwiftData model container and item for the Swift Preview to demonstrate how data would be created.</p>

To preview data, you must create a mock container that holds mock data and pass the mock data to the SwiftUI View preview.

Looking to learn more about SwiftUI, Swift, SwiftData, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, SwiftData, design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe