How to create a SwiftData Model in Swift

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

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 09/11/2023 at 15:26
Last Updated on 10/27/2023 at 11:56
<p>Three flowers that represent Swift, Swift Data and Xcode.  The text "Create a SwiftData Model" sits beneath them.</p>

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

SubscribeCheck out our SwiftData Quick Start Guide
<p>A screenshot of an iPhone showing the result of the sample app available on our open source swift GitHub repository.</p>

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
View RepositoryHow to create a SwiftLint enabled Swift XCode Project

Step One: Create a new Swift File

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

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

<p>A screenshot of Xcode showing a completed model in the SampleSwiftDataModel.swift file.</p>

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.

WWDC23: Model your schema with SwiftDataRead Apple's Documentation on the Model Macro

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.

Consult our SwiftData Quick Start GuideSearch our Blog

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.

SwiftData Quick Start Guide

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.

WWDC23: Model your schema with SwiftDataRead Apple's Documentation on the Model Macro

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 to add complex values to SwiftData models

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.

How to create a SwiftData model variable that does not persist

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

WWDC23: Model your schema with SwiftDataRead Apple's Documentation on the Model Macro

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