iphone - 存储 NSAttributedString 核心数据

标签 iphone objective-c core-data nsattributedstring

我正在尝试将 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/

相关文章:

ios - iOS 中简单的只读数据存储(易于编辑和可视化)

iPhone 企业应用程序分发 - 需要互联网连接吗?

iphone - 使用 JSON 字符串使用 RestKit 将对象手动加载到核心数据中

ios - 在 UITableView 中显示一个表中的中型数据集

ios - 如何比较 Objective-c 中的各种 NSArray?

ios - 为多个 View Controller 使用一个属性列表 (.plist)

objective-c - NSStringFromClass([MyEntityClass class]) 是否生成安全的核心数据实体名称?

iphone - css 百分比宽度 <li> 不要在纵向填充 ipod/iphone 视口(viewport)

objective-c - 使用UIImage作为@“picture”共享Facebook对话框

objective-c - 为回调匹配 CoreData 保存通知