Greg Heo

Transformable attributes in Core Data

Boring: strings and integers; fun and mysterious: transformable!

In addition to the usual types – string, float, boolean, date – you can define core data entities with an attribute of type Transformable. What is this magic type, and what does it transform into?

Add a Transformable property, generate an NSManagedObject subclass, and this is what you’ll see:

@property (nonatomic, retain) id myTransformable;

A plain old id! So in theory, you could set any object there. On 10.9, I can set my own custom NSObject subclass object to the transformable property, but when I save the context I get an exception for the unrecognized encodeWithCoder: message.

There’s the clue that the object needs to conform to NSCoding. So NSArray, NSDictionary, NSData, and others are supported out of the box.

So you can use your own class as-is — as long as you implement the required protocol methods initWithCoder: and encodeWithCoder:. Then, you and Core Data can get a room and your data will be happily persisted and realized with your classes intact.

The standard downside is that transformable attributes are stored in the SQLite backend as binary plists inside BLOBs, so you can’t query those fields directly from an NSPredicate.