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

IosCore DataNsvaluetransformer

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondontWatchMyProfileView Question on Stackoverflow
Solution 1 - IosBrad LarsonView Answer on Stackoverflow