存储 NSAttributedString 核心数据 [英] Storing NSAttributedString Core Data

查看:16
本文介绍了存储 NSAttributedString 核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 NSAttributedString 存储到 Core Data SQL 存储中.

I am trying to store an NSAttributedString to a Core Data SQL store.

我将属性设置为可转换",它是可选的,它不是瞬态或索引的,并且值转换器名称设置为默认值NSKeyedUnarchiveFromData".在 .xcdatamodel 中并生成了在 .h 中有这个的托管对象类:

I have the property set as a "transformable", it is optional and it is NOT transient or indexed and the value transformer name is set to default "NSKeyedUnarchiveFromData". In the .xcdatamodel and generated the managed object class which has this in the .h:

@property (nonatomic, retain) id Text; (I have tried changing id to NSAttributedString *Text)

这在 .m 中:

@dynamic Text;

我查看并将我的 NSManagedObject 的.text"属性设置为属性字符串,然后完成后我做:

I look through and set the ".text" property of my NSManagedObject to the attributed string then when completed I do:

NSError *error = nil;
[managedObjectContext save:&error];

此过程导致输出中出现此错误:

This through is causing this error in the output:

[NSCFType encodeWithCoder:]:无法识别的选择器发送到实例0xc04edb0 由于以下原因终止应用程序未捕获的异常'NSInvalidArgumentException',原因:'* -[NSCFType encodeWithCoder:]:无法识别的选择器发送到实例0xc04edb0'

[NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0xc04edb0 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0xc04edb0'

我已经检查了我存储到属性的内容的类,它是 NSAttributedString 我也检查了 responsesToSelector @selector(:) 并且这返回 true 所以非常困惑因为这与错误消息相反?

I have checked the class of what I am storing to the property and it is NSAttributedString also I check responsesToSelector @selector(:) and this returns true so very confused as this is contrary to the error message?

请指教.

谢谢詹姆斯

推荐答案

我正在查看 Apple 开发者论坛 并发现了一个与此问题几乎完全相同的主题,有人已经这样做了,但不幸的是没有分享代码.他们只说了以下几点:

I was checking the Apple Developer Forums and found a thread almost exactly the same as this question, one person had done this but unfortunately did not share the code. All they said was the following:

在 Core Data 中,我在数据库中有一个可转换对象,我使用自己的 NSVauleTransformer.这是 NSValueTransformer 的子类,并从数据对象创建属性字符串并返回.

"In Core Data i have an transformable in the database and i us my own NSVauleTransformer. This is a subclass of NSValueTransformer and creates an attributed string from the data object and back.

因此,我创建了一个名为 PersistableAttributedString 的类,它与 NSCoding 兼容.这个类有一个字符串和一个属性数组,并构建属性字符串.我还为符合 NSCoding 的可能的文本属性创建了一个类.字符串和属性都符合 NSCoding 标准.

Therefore i created a class called PersistableAttributedString which is NSCoding compliant. This class has a string and an array of attributes and builds the attributed string. I also created a class for the possible text attributes which is NSCoding compliant. Together the string and the attributes are all NSCoding compliant.

NSAttributedString 类也符合 NSCoding,但属性不符合,这就是问题所在."

The class NSAttributedString is also NSCoding compliant, but the attributes are not, that's the problem."

希望能有所帮助.

这篇关于存储 NSAttributedString 核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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