How to create a SwiftData 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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/26/2023 at 16:14
Last Updated on 10/27/2023 at 11:38
<p>Three flowers that represent Swift, SwiftData and Xcode side by side. Beneath them sits the text "Transient Values."</p>

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

SubscribeCheck out our SwiftData Quick Start Guide

Step One: Create a SwiftData model

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

Consult the tutorial below to learn how to setup a SwiftData model.

How to create a SwiftData Model in Swift

Step Two: Create a Transient Variable

<p>A screenshot of Xcode showing a struct that uses the codable protocol.</p>

Add the new variable that you wish not to persist, set its default value and mark it with an @Transient annotation.

How to add structs or enums to SwiftData models

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

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