Evening,
I'm trying to build an Entity A with a property x of type T.
T is an enum that I created.
How should I set xcdatamodel to use a custom type for an attribute?
My current set up is:
attribute x, type: Undefined, CustomClass: T.
But I have the error:
x .... core data must have a defined type.
Specific Purpose:
x is a "mood" attribute with 3 different moods possible, that's why I choose the enum.
There is a type called Transformable
in CoreData that's designed specifically for handling types that are not natively supported.
Basically this type will help you automatically serialize a non supported type to a Data
object before storing it in the database, and deserialize it when it is retrieved.
This is great for storage, but it can become tricky to implement when it comes to queries (because you queries are now using the raw Data
used by the database).
An easier way to implement this is to use a computed property that hides the raw value (which will usually be a String
or an Int
).
Here is how you can implement it :
enum Mood: String
{
case happy
case sad
}
class MyObject: NSManagedObject
{
@NSManaged private var rawMood: String?
var mood: Mood? {
get {
if let rawMood = self.rawMood {
return Mood(rawValue: rawMood)
} else {
return nil
}
}
set {
self.rawMood = newValue?.rawValue
}
}
}
Remember that when you are adding contraint on a query you need to use rawMood
and pass it a String
. Core Data has no knowledge of a mood
property.
There is no need to bend Core Data managed objects to accept enums, especially in this simple case.
Just use a standard String
attribute, call it mood
and do the enum evaluation and appropriate logic when you read and write to it. This will result in human readable, intuitive code.
Edit: sample implementation
enum Mood: String { case happy, ok, sad }
class Person: NSManagedObject {
var mood: String?
}
// set
person.mood = Mood.happy.rawValue
// get
let aPersonsMood = Mood(rawValue: person.mood!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With