Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store swift enum in core data?

Swift allows you to define enum but core data doesn't support (out of the box) how to save them.

The recommended solution I have seen on the internet (and used thus far) is to use a private variable:

class ManagedObjectSubClass : NSManagedObject
{
  enum Cards : Int
  {
    case Diamonds, Hearts
  }
   @nsmanaged var cardRaw: Int

   var card : Cards {
     set { self.cardRaw = newValue.rawValue }
     get { return Cards(RawValue:cardRaw)! }
   }
 }

An alternate solution is given in the answer below.

like image 960
user965972 Avatar asked Feb 13 '16 09:02

user965972


1 Answers

Another approach is to use the primitive functions. This avoid having to define two variables. In the model editor card is defined as an Int.

class ManagedObjectSubClass : NSManagedObject
{
  enum Cards : Int
  {
    case Diamonds, Hearts
  }

   var card : Cards {
     set { 
        let primitiveValue = newValue.rawValue
        self.willChangeValueForKey("card")
        self.setPrimitiveValue(primitiveValue, forKey: "card")
        self.didChangeValueForKey("card")
     }
     get { 
        self.willAccessValueForKey("card")
        let result = self.primitiveValueForKey("card") as! Int
        self.didAccessValueForKey("card")
        return Cards(rawValue:result)!
    }
   }
 }

edit:

The repetitive part can be moved to an extension on NSManagedObject.

func setRawValue<ValueType: RawRepresentable>(value: ValueType, forKey key: String)
{
    self.willChangeValueForKey(key)
    self.setPrimitiveValue(value.rawValue as? AnyObject, forKey: key)
    self.didChangeValueForKey(key)
}

func rawValueForKey<ValueType: RawRepresentable>(key: String) -> ValueType?
{
    self.willAccessValueForKey(key)
    let result = self.primitiveValueForKey(key) as! ValueType.RawValue
    self.didAccessValueForKey(key)
    return ValueType(rawValue:result)
}
like image 146
user965972 Avatar answered Nov 15 '22 07:11

user965972