How to create a SwiftData Model in Swift

Oscar de la Hera Gomez
Three flowers that represent Swift, Swift Data and Xcode.  The text "Create a SwiftData Model" sits beneath them.

A step by step guide on creating a SwiftData Model for SwiftUI, UIKit or AppKit.

A screenshot of an iPhone showing the result of the sample app available on our open source swift GitHub repository.

The following tutorial has been made available, along with a simple app for demonstrating SwiftData, through the tutorial/swift-data-basics branch on our Open Source Swift Starter Project.

git clone git@github.com:delasign/swift-starter-project.git

Step One: Create a new Swift File

A screenshot of Xcode showing a new file called SampleSwiftDataModel.swift<em>.</em>

In the XCode project, create a new file and name it after your model.

We recommend that you place this under a SwiftData folder within the Models folder.

Step Two: Create the Model

A screenshot of Xcode showing a completed model in the SampleSwiftDataModel.swift file.

Import SwiftData and create a class that uses the @Model macro.

Please note that as of September 11th 2023, the model class requires an init function. We recommend that you type out the parameters for the class and then type init within the class, allowing Xcode to auto-complete the init function.

For more information about SwiftData Models and their annotations, consult the links below.

Looking to learn more about things you can do with SwiftData, Swift or XCode ?

Consult our quick start guide or search our blog to find educational content on learning how to use SwiftData, Swift or XCode.

Frequently Asked Questions (FAQ)

Why am I getting a "No Such Module "SwiftData"" error ?

If you cannot find SwiftData, you are probably not using Xcode 15+ or do not have iOS 17+ installed on your iPhone / XCode.

Consult our Quickstart guide for links to these items.

How do I fix "Protocol 'Equatable' requires '==' to be available in iOS 16.0 and newer" or "Protocol 'Hashable' requires 'hash(into:)' to be available in iOS 16.0 and newer"?

This will appear if you have not set the projects minimum dependency to iOS17.

How do I fix "@Model requires an initializer be provided for ..."?

As of writing this article, @Model macro's require an initialization function. Within the model type init and let XCode auto-complete the initialization function.

How do I state that a model variable is unique ?

Add a @Attribute(.unique) prefix to the variable.

For other annotations within the Model macro, consult the links below.

How do I add structs or enums to a SwiftData Model ?

To include complex values such as structures, enumerations or other value types to a SwiftData model, make sure that they conform to the Codable protocol.

How create a model variable that does not persist ?

To tell SwiftData that a variable should not persist and always start at a default value on launch, use the @Transient annotation.

For other annotations within the Model macro, consult the links below.

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