Int16 master data as optional
-
In Swift, how do you make the
NSManaged Int16
be propertyoptional
like this:NSManaged var durationType: Int16?
I am getting compiler error:
roperty cannot be marked @NSManaged because its type cannot be represented in Objective-C
-
If this is not possible and I check the window
optional
in the Core Data Model Editor, how can I check if this property has a value when I exit the database?
source to share
You can make the property optional and save it Int16
. The key is that it is @NSManaged
not required, but if you remove it, you must implement your own accessor methods.
One possible implementation:
var durationType: Int16?
{
get {
self.willAccessValueForKey("durationType")
let value = self.primitiveValueForKey("durationType") as? Int
self.didAccessValueForKey("durationType")
return (value != nil) ? Int16(value!) : nil
}
set {
self.willChangeValueForKey("durationType")
let value : Int? = (newValue != nil) ? Int(newValue!) : nil
self.setPrimitiveValue(value, forKey: "durationType")
self.didChangeValueForKey("durationType")
}
}
source to share