简体   繁体   中英

JSON to core-data

I am trying to save JSON response to local core-data for my app. I am following the following implementation for this: 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. 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.

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.

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.

I'm not seeing setValuesForKeysWithDictionary in the dev center for NSManagedObject

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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