-1

I have to store dictionary in core data. I never worked with core data before.

Can anyone help me out that how can I use core data in my existing project and use it to store Dictionary.

Matthias
  • 3,582
  • 2
  • 30
  • 41
Gourav Gupta
  • 41
  • 1
  • 7
  • after some google i get to know that i have to do archive and un-archive for doing this. But i didn't know how to implement it in project. – Gourav Gupta Mar 12 '14 at 05:06
  • have you seen [THIS](http://stackoverflow.com/questions/22211633/core-data-not-saving-transformable-nsmutabledictionary) – Dan Shelly Mar 12 '14 at 06:17
  • Check out: [insert NSDictionary into CoreData](http://stackoverflow.com/questions/8682324/insert-nsdictionary-into-coredata) – Amar Mar 12 '14 at 06:54

1 Answers1

5

NSDictionary into Core Data like that you need to do

 NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:yourDictionary forKey:@"dictpropertyinmanagedobject"];
    [archiver finishEncoding];
    [archiver release];
    [self.managedObject setValue:data forKey:@"dictpropertyinmanagedobject"];

NSDictionary from Core Data

NSData *data = [[NSMutableData alloc] initWithData:[yourManagedObject valueForKey:@"dictpropertyinmanagedobject"]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *yourDictionary=[[unarchiver decodeObjectForKey:@"dictpropertyinmanagedobject"] retain];
[unarchiver finishDecoding];
Sport
  • 8,570
  • 6
  • 46
  • 65