在Core Data中存储UIColor对象 [英] Storing UIColor object in Core Data

查看:232
本文介绍了在Core Data中存储UIColor对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个UIColor对象作为一个属性存储在一个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.

在我的研究中,虽然文献似乎很稀疏,我发现这篇文章说,属性应该指定为可变换,并且数据应手动转换使用 NSKeyedArchiver 使用 NSKeyedUnarchiver

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.

但是,我发现一个更近的帖子说,所有必要的是将UIColor保存为CD中的Transformable属性,生成Managed Object子类,并像其他属性一样使用。

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.

苹果文档自身进入一些细节存储和检索 NSColor 对象,但不是(我可以找到)在 UIColor 。然而,它使它听起来像归档/取消归档在CD内自动处理。也许。我对此行感到困惑:

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:

如果您在Xcode中使用模型编辑器,请在属性的类型弹出窗口中选择Transformable并在Value Transformer中键入名称名称文本字段。

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:

重要提示:尽管默认变压器是由NSKeyedUnarchiveFromDataTransformerName指定的变压器,但此变压器实际上反过来使用。如果你明确指定默认转换器,Core Data将使用它在错误的方向。

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."

我已经到了我有一个名为color的新的可变换属性(未命名变换器),并生成了包含新颜色属性(类型id)的新子类文件。

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?

我使用MagicalRecord与Core Data接口,如果这很重要。

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

非常感谢您的帮助!

推荐答案

您的属性应为Transformable类型。默认值转换器(NSKeyedUnarchiveFromDataTransformerName)可以转换符合NSCoding的任何对象。

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


  1. 将属性标记为类型Tranformable。

  2. 可选:将值变换器名称NSKeyedUnarchiveFromDataTransformerName。如果你不这样做,它将默认为this。

您不必执行其他操作。请记住,您将无法使用谓词匹配transformable属性或通过它们排序。它们几乎只是存储 - 值转换器将对象值转换为NSData,这是在存储中持久保存的。当属性故障触发Core Data使用其他方向的变换器从NSData到您的对象类型。

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.

这篇关于在Core Data中存储UIColor对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆