Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

ios - Storing UIColor object in Core Data

I want to store a UIColor object as an attribute on a Core Data entity, then retrieve it and use it in the same fashion that I do the other attributes on that entity.

In my research, while the literature seems pretty sparse, I find this post saying that the attribute should be specified as "Transformable," and that the data should be manually converted going in using NSKeyedArchiver and coming out using NSKeyedUnarchiver.

But then, I find a more recent post saying that all that's necessary is to save the UIColor as a Transformable attribute in CD, generate a Managed Object subclass, and use just like other attributes.

The Apple docs themselves go into some detail on storing and retrieving NSColor objects, but not (that I could find) on UIColor. However, it makes it sound like the archiving/unarchiving is handled automatically within CD. Maybe. I was confused by this line:

If you are using the model editor in Xcode, select Transformable in the attribute’s Type popup and type the name in the Value Transformer Name text field.

I assume they're talking about the name of the attribute, but am not sure, especially since it warns about potential problems arising from specifying the transformer explicitly:

Important: Although the default transformer is the transformer specified by NSKeyedUnarchiveFromDataTransformerName, this transformer is actually used in reverse. If you specify the default transformer explicitly, Core Data would use it “in the wrong direction.”

I've gotten to the point where I have a new transformable attribute named "color," (did not name the transformer) and have generated a new subclass file containing the new "color" property (type id).

I'm just plain perplexed. Can someone please give me some definitive guidance on how to proceed from here?

I'm using MagicalRecord to interface with Core Data, if that matters.

Many thanks for helping!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your attribute should be of the type Transformable. The default value transformer (NSKeyedUnarchiveFromDataTransformerName) can transform any object that conforms to NSCoding.

  1. Mark the attribute as type "Tranformable".
  2. Optional: Set the Value Transformer Name to "NSKeyedUnarchiveFromDataTransformerName". If you do not, it will default to this anyway.

Like so

You do not have to do anything else. Keep in mind you will not be able to match transformable attribute with a predicate or sort by them. They are pretty much just storage - the value transformer transforms the object value into NSData, which is what gets persisted in the store. When the attribute fault fires Core Data uses the transformer in the other direction to go from NSData to your object type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...