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

查看:19
本文介绍了在 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 中,生成一个托管对象子类,并像其他属性一样使用.

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.

Apple 文档本身 详细介绍了存储和检索 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 中使用模型编辑器,请在属性的类型弹出窗口中选择可转换并在值转换器名称文本字段中输入名称.

我假设他们在谈论属性的名称,但我不确定,特别是因为它警告了显式指定转换器引起的潜在问题:

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:

重要:虽然默认的transformer是NSKeyedUnarchiveFromDataTransformerName指定的transformer,但是这个transformer其实是反过来使用的.如果您明确指定默认转换器,Core Data 将以错误的方向"使用它.

我已经有了一个名为color"的新可转换属性(未命名转换器),并生成了一个包含新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. 将属性标记为可转换"类型.
  2. 可选:将值转换器名称设置为NSKeyedUnarchiveFromDataTransformerName".如果你不这样做,无论如何它都会默认为这个.

您无需执行任何其他操作.请记住,您将无法将可转换属性与谓词匹配或按它们排序.它们几乎只是存储 - 值转换器将对象值转换为 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天全站免登陆