存储NSAttributedString核心数据 [英] Storing NSAttributedString Core Data
问题描述
我试图将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)
>
and this in the .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 @
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?
请告诉我们。
感谢
James
Thanks James
推荐答案
论坛,并发现一个线程几乎完全相同的问题,一个人这样做,但不幸的是没有共享代码。他们说的是以下:
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,这是
"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屋!