How to add complex values to SwiftData models
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
Consult the tutorial below to learn how to setup a SwiftData model.
Step Two: Create the Complex Value
Create a new struct, enumeration or complex value and make sure that it uses the Codable protocol.
Step Three: Add to the SwiftData Model
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.