Swift: Storing states in CoreData with enums

后端 未结 2 1786
灰色年华
灰色年华 2020-11-30 23:36

I want to store an enum state for a managed object within CoreData

enum ObjStatus: Int16 {
    case State1 = 0
    case State2 = 1
    case State3 = 3
}

cla         


        
2条回答
  •  长情又很酷
    2020-12-01 00:11

    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.

提交回复
热议问题