简体   繁体   English

JSON到核心数据

[英]JSON to core-data

I am trying to save JSON response to local core-data for my app. 我正在尝试为我的应用程序保存对本地核心数据的JSON响应。 I am following the following implementation for this: https://stackoverflow.com/a/2363996/127036 为此,我正在遵循以下实现: https : //stackoverflow.com/a/2363996/127036

Here is my code: 这是我的代码:

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

Compiler returns following error: 编译器返回以下错误:

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

While trying to execute: 尝试执行时:

setValuesForKeysWithDictionary

Please guide me in the right direction to fix this issue. 请引导我朝正确的方向解决此问题。

This post is quite old now and I'm sure the Vibhor Goyal has already moved on. 这篇文章现在已经很老了,我确定Vibhor Goyal已经发展了。 But in case anyone runs into this same problem, the reason why he's getting 但是如果有人遇到同样的问题,他之所以得到

-[__NSCFDictionary entity]: unrecognized selector sent to instance

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. 是因为在他的Post模型中,我确定他具有子实体,可能类似于“评论”之类,这可能与“评论”模型具有许多关系。

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. 然后,在他的structureDictionary中,他可能具有与Post模型相对应的常规键值字段,并且对于“ comments”键,他可能具有值的NSDictionaries NSSet。

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. 因此,他得到该实体选择器错误的原因是,在后台,核心数据期望“注释”是一组填充“ Comment”实例的集合,这些实例应该是NSManagedObject实例,但实际上它正在获取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. 希望这对以后的人有所帮助。

I'm not seeing setValuesForKeysWithDictionary in the dev center for NSManagedObject 我在NSManagedObject的开发中心没有看到setValuesForKeysWithDictionary

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html

You may have to loop through and do them one at a time with setValue:forKey: 您可能需要遍历并一次使用setValue:forKey一次执行一次:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM