views:

449

answers:

1

I've been using transformable attributes in core data to transform complex objects like images and colors into raw data. I took this...

The idea behind transformable attributes is that you access an attribute as a non-standard type, but behind the scenes Core Data uses an instance of NSValueTransformer to convert the attribute to and from an instance of NSData. Core Data then stores the data instance to the persistent store.

... to mean that you could only convert things to and from data and not one of the other types of attributes such as strings.

It just occurred to me that this might not be the case. The documentation might just be talking about the most common case. IIRC, in Cocoa bindings, the transforms can be largely arbitrary. It is possible to transform, say, a NSURL to a NSString for display and then reverse it.

Before I waste time experimenting and possibly getting a confusing result, I wondered if someone knew for certain if the transform is only to and from data only.

+1  A: 

Correct. You must transform your attribute into an NSData object. You would need to serialize an NSURL to NSData -- and the default NSKeyedUnarchiveFromDataTransformerName transformer will do this for you.

Another approach, and the one that I use for URLs, is to maintain two parallel properties. One transient property of undefined type for the URL, and a second persistent property of string type for the backing store. I lazily construct the URL from the string the first time it's requested, and I update the string property whenever the URL is changed.

There's no way to enforce it, but you really don't want to use the string property from outside your entity's class. I generally make the @property definition for the string attribute private to remind myself not to use it.

Alex
Yes, I've been using the same approach for sometime now I just wanted to check that I hadn't misunderstood something.
TechZen