问题
iOS 10 introduces new classes for measurements and units. I would like to either store a NSMeasurement or NSUnit object in my core data database, so that I can store different measurements in my database.
Is there a good way to do this? Since my app would only use a subset of all available units, I could in theory create a string representation of the units and use a switch statement to get the actual unit classes from that when needed. But that feels overly complicated and error prone.
回答1:
Both of those classes conform to NSCoding, so you can a store them as "transformable" attributes. As transformables, you read/write object values directly from/to the attribute, and Core Data automatically converts between the object type and NSData as needed. You don't need to do any conversions of your own.
回答2:
Archiving an NSUnit will add a lot of storage and performance overhead. Therefore it might be preferrable to store the values as scalar types of the appropriate NSUnit's baseUnit and use an NSMeasurementFormatter to display values in the UI.
来源:https://stackoverflow.com/questions/38423243/store-nsmeasurement-or-nsunit-in-core-data