Are enums with associated values supported by swiftdata in any real way?

I haven’t found any way to have a swift data model contain an enum with different associated values, each different types of models, in order to deal with models that require heterogenous collections. Is this simply not supported? Is it supported but not ever used with SwiftUI binding? Instead I’ve had to create my own sort of enum in the form of a model that contains optionals for every case but this is obviously not ideal.

Answered by franktinsley in 760676022

I found a workaround for this that is similar to other workarounds for enums where you store the data in a private property and have a computed property that gets and sets it using the type you really wanted to use. My stored property is a string and my computed property encodes and decodes to and from json. I assume this is essentially what SwiftData itself will do when they finally add support for enums with associated values.

you just need to make you enum "Codable" to use that in SwiftData Model

Accepted Answer

I found a workaround for this that is similar to other workarounds for enums where you store the data in a private property and have a computed property that gets and sets it using the type you really wanted to use. My stored property is a string and my computed property encodes and decodes to and from json. I assume this is essentially what SwiftData itself will do when they finally add support for enums with associated values.

Are enums with associated values supported by swiftdata in any real way?
 
 
Q