问题
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