将NSMeasurement或NSUnit存储在核心数据中 [英] Store NSMeasurement or NSUnit in Core Data
问题描述
iOS 10引入了用于测量和单位的新类。我想将NSMeasurement或NSUnit对象存储在我的核心数据数据库中,以便可以在数据库中存储不同的测量值。
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.
有什么好方法吗?这个?由于我的应用仅使用所有可用单元的子集,因此从理论上讲,我可以创建单元的字符串表示形式,并在需要时使用switch语句从中获取实际的单元类。但这感觉太复杂且容易出错。
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.
推荐答案
这两个类均符合 NSCoding
,因此您可以将它们存储为可转换属性。作为可转换对象,您可以直接在属性中读取/写入对象值,然后,Core Data会根据需要在对象类型和 NSData
之间自动进行转换。您不需要自己进行任何转换。
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.
这篇关于将NSMeasurement或NSUnit存储在核心数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!