How does one save and retrieve a generic Measurement in Core Data?
What I'm looking to do is save either a Measurement<UnitMass> or a Measurement<UnitVolume>.
As can be seen in the image below CoreData is set to accept a Generic Measurement<Unit>

However I'm getting an error when I go to set the value of the measure. Saying that I'm not allowed to do this. I thought the purpose of generics was to support uses like this.
What am i missing?

The error about mismatched types isn't the important part here. The real problem is that transformable attributes only work with classes that conform to NSCoding or for which you've written your own custom value transformer. Since Measurement is not a class and does not conform to NSCoding, you can't use it with a transformable attribute.
Your options are
Measurement, save its values, and convert to/from the Measurement when saving/reading property values.ValueTransformer that will convert between Measurement and Data.I'd go with #1. You could add convenience methods on your managed object subclass to handle the conversion.
Update: Using your Measurement<UnitMass> case, I'd do something like:
Double property named massValue.massUnit with custom class UnitMass (see below).Save values with something like this:
let servingMeasure = Measurement<UnitMass>(value:500, unit:.grams)
myObject.massValue = servingMeasure.value
myObject.massUnit = servingMeasure.unit
Retrieve values with something like:
if let unit = myObject.massUnit {
let value = myObject.massValue
let measurement = Measurement<UnitMass>(value:value, unit:unit)
print("Measurement: \(measurement)")
}
This is how the massUnit property is configured:

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