Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data with json

This question is a follow up on this question. I am using the JSON Library found at http://code.google.com/p/json-framework/.


My Core Data object model has a many-to-many relationship to itself and as such, has a set for its sub object. In JSON, the set is represented through an array of object ids. Nothing really exotic.

When I am calling setValuesForKeysWithDictionary on the managed object with the object structure I get from parsing the json string, I receive this exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM minusSet:]: unrecognized selector sent to instance 0x6c7b440'

If someone can explain why, I'm all ears. I also receive some exception from undefined key but this is understandable(JSON contains extra fields) and totally manageable.

Now my question is:

Am I missing something here because in the other question, the person who answered and OP didn't report any of this problem. I could patch it and handle the faulty operation by overriding setValuesForKeysWithDictionary and passing when the key is a relationship but this makes the code a lot less generic which I quite like.

like image 980
Eric Fortin Avatar asked Oct 10 '10 13:10

Eric Fortin


1 Answers

I think the problem is that your JSON deserialization is creating arrays, but managed object to-many relationships are represented by sets. I.e. you need NSSet rather than NSArray. You can convert an NSArray to an NSSet by doing [NSSet setWithArray:theArray].

like image 195
Daniel Dickison Avatar answered Sep 29 '22 08:09

Daniel Dickison