我正在尝试将 NSAttributedString 存储到核心数据 SQL 存储。
我将属性设置为“可转换”,它是可选的,它不是 transient 或索引的,值转换器名称设置为默认“NSKeyedUnarchiveFromData”。在 .xcdatamodel 中并生成了在 .h 中具有此内容的托管对象类:
@property (nonatomic, retain) id Text; (I have tried changing id to NSAttributedString *Text)
这在 .m 中:
@dynamic Text;
我查看并将我的 NSManagedObject 的“.text”属性设置为属性字符串,然后在完成后我这样做:
NSError *error = nil;
[managedObjectContext save:&error];
此通过导致输出中出现此错误:
[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 非常困惑因为这与错误消息相反?
请指教。
谢谢 詹姆斯
最佳答案
对于遇到此问题的任何人,我找到了最简单的解决方案:
在 Core Data 中添加一个属性,我们称它为 attributedText
。然后将其类型定义为Transformable
。创建 .h 文件后,将 attributedText
的数据类型从 NSDictionary
更改为 NSAttributedString
。
现在您可以将 NSAttributedString
保存在 Core Data 中,无需修改。
记忆起来很容易:
myObject.attributedText
这将返回您的 NSAttributedString
!
希望这对某人有帮助。
关于iphone - 存储 NSAttributedString 核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266226/