Best way to implement Enums with Core Data

前端 未结 9 1607
天命终不由人
天命终不由人 2020-11-28 17:55

What is the best way to bind Core Data entities to enum values so that I am able to assign a type property to the entity? In other words, I have an entity called Item<

9条回答
  •  孤独总比滥情好
    2020-11-28 18:28

    Since enums are backed by a standard short you could also not use the NSNumber wrapper and set the property directly as a scalar value. Make sure to set the data type in the core data model as "Integer 32".

    MyEntity.h

    typedef enum {
    kEnumThing, /* 0 is implied */
    kEnumWidget, /* 1 is implied */
    } MyThingAMaBobs;
    
    @interface myEntity : NSManagedObject
    
    @property (nonatomic) int32_t coreDataEnumStorage;
    

    Elsewhere in code

    myEntityInstance.coreDataEnumStorage = kEnumThing;
    

    Or parsing from a JSON string or loading from a file

    myEntityInstance.coreDataEnumStorage = [myStringOfAnInteger intValue];
    

提交回复
热议问题