Store NSMeasurement or NSUnit in Core Data

北城以北 提交于 2019-12-22 23:20:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!