在字典中存储NSManagedObject(NSDictionary) [英] Storing NSManagedObject in a dictionary (NSDictionary)

查看:181
本文介绍了在字典中存储NSManagedObject(NSDictionary)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类,它是 NSManagedObject 的一个子类。我想把它存储在字典,但当尝试这样做,我收到一个属性列表无效的格式:200 错误。

I have a custom class, which is a subclass of NSManagedObject. I would like to store it in a dictionary, but when trying to do so I receive a Property list invalid for format: 200 error.

这是我如何尝试创建字典:

Here is how I try to create the dictionary:

NSDictionary *dictionary = 
    [NSDictionary dictionaryWithObject: voiceMemo forKey:@"voiceMemo"];

尝试

NSDictionary *dictionary = 
    [NSDictionary dictionaryWithObject: (NSData *) voiceMemo forKey:@"voiceMemo"];

但是,当试图单独保存各个属性时,它可以工作:

It works, however, when trying to save the individual attributes separately:

NSDictionary *dictionary = 
    [NSDictionary dictionaryWithObject: voiceMemo.attribute forKey:@"attribute"];

NSDictionary 应该能够存储数据对象,所以我猜,真正的问题是如何铸造 NSManagedObject 对象到 NSData

NSDictionary should be able to store data objects, so I'm guessing, the real question is how to cast an NSManagedObject object to NSData

推荐答案

您确定在创建字典时发生了错误,就像您发布的代码所暗示的那样?

Are you sure the error happens when you create the dictionary like it is implied by the code you have posted?

属性列表无效的格式:200 听起来像你试图写你的 NSManagedObject 文件系统。这不会工作,因为 NSManagedObjects 不确认 NSCoding

Property list invalid for format: 200 sounds like you try to write your NSManagedObject to the file system. Which won't work, because NSManagedObjects don't confirm to NSCoding.

您可以将NSManagedObject的属性保存在NSDictionary中,并将此字典保存到文件中。

You could save the attributes of the NSManagedObject in a NSDictionary, and save this dictionary to a file..

NSArray *keys = [[[myObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];

,当你想恢复它时,你像这样设置managedObject的值:

and when you want to restore it you set the values of your managedObject like this:

[myObject setValuesForKeysWithDictionary:dict];

这篇关于在字典中存储NSManagedObject(NSDictionary)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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