ios 我可以用 iPhone 上 Core Data 中的 Transformable 属性类型做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3014498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What can I do with an Transformable attribute type in Core Data on the iPhone?
提问by dontWatchMyProfile
There's this Transformable data type for attributes. What is it good for? Are there good examples?
属性有这种 Transformable 数据类型。到底有什么好处呢?有好的例子吗?
I'd like to play around with this. So after searching a while I came across this: NSValueTransformer. Seems to be something I need for this.
我想玩这个。所以在搜索了一段时间后,我发现了这个:NSValueTransformer。似乎是我需要的东西。
So how would I get started with this? For example, if I wanted to store an UIColor object, would I make an transformer for that?
因此,我将如何开始使用呢?例如,如果我想存储一个 UIColor 对象,我会为此制作一个转换器吗?
What exactly is this thing transforming to or from? An NSData? And must the object which I pass to the transformer follow any protocol?
这东西究竟是从什么转变过来的?一个 NSData?我传递给变压器的对象是否必须遵循任何协议?
回答by Brad Larson
Transformable attributes are useful for storing nonstandard object types within Core Data. For example, I provide code in this answerthat lets you store UIImages as an attribute within Core Data. The image data is converted to and from an NSData instance that contains the image's PNG representation. This is all handled transparently for you by a custom NSValueTransformer.
可转换属性对于在 Core Data 中存储非标准对象类型很有用。例如,我在这个答案中提供了代码,让您将 UIImages 作为属性存储在 Core Data 中。图像数据与包含图像的 PNG 表示的 NSData 实例相互转换。这一切都由自定义 NSValueTransformer 为您透明处理。
You may also wish to encrypt individual attributes within your Core Data model, as I describe here. Using a transformable attribute for this makes this trivial to code.
您可能还希望加密核心数据模型中的各个属性,如我在此处所述。为此使用可转换属性使代码变得微不足道。