JSON到核心数据 [英] JSON to core-data

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

问题描述

我试图为我的应用程序保存JSON响应本地核心数据。我为此遵循以下实现:
http://stackoverflow.com/a/2363996/127036

I am trying to save JSON response to local core-data for my app. I am following the following implementation for this: http://stackoverflow.com/a/2363996/127036

这是我的代码:

NSString *objectName = @"Post";
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:objectName inManagedObjectContext:moc];
[managedObject setValuesForKeysWithDictionary:structureDictionary];

编译器返回以下错误:

-[__NSCFDictionary entity]: unrecognized selector sent to instance 0x6dc6fa0

尝试执行:

setValuesForKeysWithDictionary

请指导我朝正确的方向解决这个问题。

Please guide me in the right direction to fix this issue.

推荐答案

现在,我确信Vibhor Goyal已经走了。但是,如果有人遇到同样的问题,他为什么会得到

This post is quite old now and I'm sure the Vibhor Goyal has already moved on. But in case anyone runs into this same problem, the reason why he's getting

-[__NSCFDictionary entity]: unrecognized selector sent to instance

因为在他的Post模型中,我确定他有子实体, 这可能是一个对一个注释模型的一对多的关系。

Is because in his Post model, I'm sure he has sub entities, probably something like "comments" which is probably a to-many relationship to a "Comment" model.

然后在他的structureDictionary,他可能有通常的键值字段对应他的帖子模型,对于评论键,他可能有一个NSSet的NSSet的值。

Then in his structureDictionary, he probably has the usual key-value fields that corresponds to his Post model, and for the "comments" key he probably has a NSSet of NSDictionaries for the value.

所以他得到实体选择器错误的原因是因为,核心数据期望注释是一个充满注释实例的集合,这些实例应该是NSManagedObject的实例,而是获得NSDictionary的实例。因此,当它从每个评论中要求实体时,它会抛出无法识别的选择器错误。

So the reason why he's getting that entity selector error is because under the hood, core data is expecting "comments" to be a set filled with instances of "Comment" which are supposed to be instances of NSManagedObject, but instead it's getting instances of NSDictionary. So when it asks for "entity" from each of the "comments" it throws the unrecognized selector error.

希望这有助于未来的人。

Hope this helps someone in the future.

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

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