openfrog
openfrog

Reputation: 40755

Can I use a custom class for holding data of an attribute in Core Data?

Apple is giving the example, that I could make a MyColor class for holding color data, and use this with an NSAttributeDescription object by calling the -setAttributeValueClassName: method.

But what's missing there is this: How's MyColor persistet? And what do I have to provide in -setAttributeType: when I do that? There's no type like "custom class" or something this way.

Would Core Data just serialize MyColor and store that in an String data type somehow? How does that work?

Edit: Does this have to do anything with value transformers?

Upvotes: 2

Views: 418

Answers (1)

Brad Larson
Brad Larson

Reputation: 170317

If you read that same documentation, you see that they recommend using transformable attributes for custom classes:

Note: The example for an object value uses an instance of NSColor; if you are using Mac OS X v10.5, you should typically use a transformable attribute instead.

The iPhone OS 3.0 Core Data is similar to Snow Leopard era Core Data, so the above statement applies to it as well.

You will want to create an NSValueTransformer that will convert your custom type to a type that Core Data can handle, like NSData. This CocoaDev wiki page links to some examples of NSValueTransformer. I provide some code that I use for transforming UIImages to PNG data in this answer.

Upvotes: 3

Related Questions