How to preview SwiftData in XCode Previews

Oscar de la Hera Gomez
Three flowers that represents SwiftUI, SwiftData and XCode side by side with the text “Preview SwiftData” beneath it.

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

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

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

Preview Actions

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

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

Preview Data

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.

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.

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.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details