开发者

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

开发者 https://www.devze.com 2022-12-16 20:48 出处:网络
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.

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开发者_如何学Go 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号