What can I do with an Transformable attribute type in Core Data on the iPhone?

十年热恋 提交于 2019-12-03 01:27:02

问题


There's this Transformable data type for attributes. What is it good for? Are there good examples?

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.

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?

What exactly is this thing transforming to or from? An NSData? And must the object which I pass to the transformer follow any protocol?


回答1:


Transformable attributes are useful for storing nonstandard object types within Core Data. For example, I provide code in this answer that 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.

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.



来源:https://stackoverflow.com/questions/3014498/what-can-i-do-with-an-transformable-attribute-type-in-core-data-on-the-iphone

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