How to add complex values to SwiftData models

Oscar de la Hera Gomez
Three flowers that represent Swift, SwiftData and Xcode. Beneath them sits the text "Complex Values."

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.

Step One: Create the Model

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

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

Step Two: Create the Complex Value

A screenshot of Xcode showing a struct that uses the codable protocol.

Create a new struct, enumeration or complex value and make sure that it uses the Codable protocol.

Step Three: Add to the SwiftData Model

A screenshot of Xcode showing how we added a complex value to the model and init function.

Add the complex value to the SwiftData model and init function.

If you do not wish for the value to persist, add a @Transient annotation and a default value.

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.

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