insert NSDictionary into CoreData

OXXY picture OXXY · Dec 30, 2011 · Viewed 23.5k times · Source

I have an NSDictionary and a CoreData Database. I want to insert the NSDictionary into the database.

  1. How can I do this (code snippets if possible)?

  2. What is the suitable type for the attribute of the dictionary ?

Answer

Vincent Bernier picture Vincent Bernier · Dec 30, 2011

You would need to serialize your NSDictionary to an NSData, CoreData can hold to NSData.
But you won't be able to search (predicate) element of your NSDictionary.

And if we think about this, NSDictionary is a collection of data.
A Table in a database is kind of a collection of data.
In CoreData the closest thing you got to a collection of data is NSManagedObject.

So my advice would be to make a NSManagedObject subclass that would hold the information you have in your NSDictionary. The key would be the attribute and the value would be the value of that attribute. And you would be able to search based on the attribute of that NSManagedObject subclass.