Usage of enum when migrating from CoreData

In the "old" world we could define a model property as an integer (e.g. "Integer 64") and then have the actual class representing that model define the property as an enum instead and have getters/setters using methods such as primitiveValue(forKey:)

When migrating to use SwiftData I changed the model to be an actual enum type and even though the underlying type of the enum is an integer and therefore supported by the primitive storage it requires me to use Codable on the enum instead and doing so makes it incompatible with the old CoreData model.

Does anyone have any ideas or workarounds here or do you feel it is a bug and should be reported?

enum FooType: Int64 {
    case awesome
    case super
}

@Model
final class Note {
    var type: FooType  // ERROR
}

Replies

Hi @davidrothera,

enums must comform to Codable. The following code should compile:

enum FooType: Int64, Codable {
    case awesome
    case great
}

@Model
final class Note {
    var type: FooType
}

See https://developer.apple.com/documentation/swiftdata/preservingyourappsmodeldataacrosslaunches#Turn-classes-into-models-to-make-them-persistable

Thanks, the issue here instead is that the "old" (CoreData) type was an Integer 64 and changing the new model to have a type of FooType: Int64 Codable isn't compatible.

What is the solution here? I’m running into the same problem.