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
with an itemType
property that I want to be bound to an enum, what is the best way of going about this.
Objective-c – Best way to implement Enums with Core Data
cocoacocoa-touchcore-dataiphoneobjective c
Best Answer
You'll have to create custom accessors if you want to restrict the values to an enum. So, first you'd declare an enum, like so:
Then, declare getters and setters for your property. It's a bad idea to override the existing ones, since the standard accessors expect an NSNumber object rather than a scalar type, and you'll run into trouble if anything in the bindings or KVO systems try and access your value.
Finally, you should implement
+ keyPathsForValuesAffecting<Key>
so you get KVO notifications for itemTypeRaw when itemType changes.