Swift: Storing states in CoreData with enums

You can declare your enum as @objc. Then it all automagically works. Here’s a snippet from a project I’m working on.

// Defined with @objc to allow it to be used with @NSManaged.
@objc enum AgeType: Int32
{
    case Age                = 0
    case LifeExpectancy     = 1
}

/// The age type, either Age or LifeExpectancy.
@NSManaged var ageType: AgeType

In the Core Data model, ageType is set to type Integer 32.

Leave a Comment