2014-03-12 63 views
-1

我必须将字典存储在核心数据中。我以前从未使用过核心数据。将NSDictionary保存在coreData中

任何人都可以帮助我,我怎样才能在我现有的项目中使用核心数据,并用它来存储Dictionary。

+0

- 为此做好准备。但我不知道如何在项目中实现它。 –

+0

你见过[THIS](http://stackoverflow.com/questions/22211633/core-data-not-saving-transformable-nsmutabledictionary) –

+0

检出:[将NSDictionary插入CoreData](http://stackoverflow.com/questions/8682324/insert-nsdictionary-into-coredata) – Amar

回答

4

的NSDictionary到核心数据一样,你需要一些谷歌我了解,我必须做的归档和取消后做

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从核心数据

NSData *data = [[NSMutableData alloc] initWithData:[yourManagedObject valueForKey:@"dictpropertyinmanagedobject"]]; 
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
NSDictionary *yourDictionary=[[unarchiver decodeObjectForKey:@"dictpropertyinmanagedobject"] retain]; 
[unarchiver finishDecoding]; 
+0

我可以在任何类中定义self.managedObject。 –

+1

看看这个http://stackoverflow.com/questions/8682324/insert-nsdictionary-into-coredata – Sport