将 NSDictionary 插入 CoreData

发布于 2024-12-23 15:34:38 字数 151 浏览 1 评论 0原文

我有一个 NSDictionary 和一个 CoreData 数据库。我想将 NSDictionary 插入数据库。

  1. 我该如何做到这一点(如果可能的话,请提供代码片段)?

  2. 字典的属性适合什么类型?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不念旧人 2024-12-30 15:34:38

您需要将 NSDictionary 序列化为 NSData,CoreData 可以保存 NSData。
但是您将无法搜索 NSDictionary 的(谓词)元素。

如果我们考虑一下这一点,NSDictionary 就是一个数据集合。
数据库中的表是一种数据集合。
在 CoreData 中,最接近数据集合的是 NSManagedObject。

所以我的建议是创建一个 NSManagedObject 子类来保存 NSDictionary 中的信息。 key 将是属性,值将是该属性的value。您将能够根据该 NSManagedObject 子类的属性进行搜索。

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.

江城子 2024-12-30 15:34:38

我找到了另一种通过创建数据类型为“Transformable”的属性将字典添加到 Coredata 中的方法。

例如,在您的项目中创建一个实体&数据类型为 Transformable 的属性。
生成 NSManagedObject 的子类。属性将可用数据类型“id”,更改为 NSDictionary。

所做的事情(我的 NSManagedObject 子类名称是“DictTest”)

-(void)InsertIntoDataBase
{
    DictTest *entityDict=(DictTest*)[NSEntityDescription insertNewObjectForEntityForName:@"DictTest" inManagedObjectContext:self.managedObjectContext];
    NSMutableDictionary *mutDict=[NSMutableDictionary dictionary];
    [mutDict setValue:@"1" forKey:@"1"];
    [mutDict setValue:@"2" forKey:@"2"];
    [mutDict setValue:@"3" forKey:@"3"];
    [mutDict setValue:@"4" forKey:@"4"];
    [mutDict setValue:@"5" forKey:@"5"];
    [mutDict setValue:@"6" forKey:@"6"];
    [mutDict setValue:@"7" forKey:@"7"];
    [mutDict setValue:@"8" forKey:@"8"];
    [mutDict setValue:@"9" forKey:@"9"];
    [mutDict setValue:@"10" forKey:@"10"];
    [entityDict setInfoDict:mutDict];
    NSError *error;
    if(![self.managedObjectContext save:&error])
    {
        NSLog(@"error description is : %@",error.localizedDescription);
    }
    else{
        NSLog(@"Saved");

    }
}

下面是我为获取记录

-(void)FetchRecord
{
    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"DictTest" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
     NSArray *fetchArray=  [self.managedObjectContext executeFetchRequest:request error:nil];
    for (DictTest *obj in fetchArray) {
        NSLog(@"Dict is : %@",obj.infoDict);
    }
}

I found another way to add Dictionary into Coredata by creating an attribute with data type 'Transformable'.

For example create an entity in your project & attribute with data type Transformable.
Generate subclass for NSManagedObject. Attribute will be available with data type 'id', change into NSDictionary.

Below is what I did (My NSManagedObject sub class name is 'DictTest')

-(void)InsertIntoDataBase
{
    DictTest *entityDict=(DictTest*)[NSEntityDescription insertNewObjectForEntityForName:@"DictTest" inManagedObjectContext:self.managedObjectContext];
    NSMutableDictionary *mutDict=[NSMutableDictionary dictionary];
    [mutDict setValue:@"1" forKey:@"1"];
    [mutDict setValue:@"2" forKey:@"2"];
    [mutDict setValue:@"3" forKey:@"3"];
    [mutDict setValue:@"4" forKey:@"4"];
    [mutDict setValue:@"5" forKey:@"5"];
    [mutDict setValue:@"6" forKey:@"6"];
    [mutDict setValue:@"7" forKey:@"7"];
    [mutDict setValue:@"8" forKey:@"8"];
    [mutDict setValue:@"9" forKey:@"9"];
    [mutDict setValue:@"10" forKey:@"10"];
    [entityDict setInfoDict:mutDict];
    NSError *error;
    if(![self.managedObjectContext save:&error])
    {
        NSLog(@"error description is : %@",error.localizedDescription);
    }
    else{
        NSLog(@"Saved");

    }
}

for Fetching records

-(void)FetchRecord
{
    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"DictTest" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
     NSArray *fetchArray=  [self.managedObjectContext executeFetchRequest:request error:nil];
    for (DictTest *obj in fetchArray) {
        NSLog(@"Dict is : %@",obj.infoDict);
    }
}
风追烟花雨 2024-12-30 15:34:38

设置实体描述,插入新对象,然后使用:

[managedObject setValuesForKeysWithDictionary:dict];

Set up your entity description, insert a new object, then use:

[managedObject setValuesForKeysWithDictionary:dict];
一影成城 2024-12-30 15:34:38

为什么不使用 NSDictionary 中的所有数据创建一个实体,然后对其进行解析。

检查这个获取一些 CoreData 代码片段。 您可以简单地创建一些实体来存储字典的信息。然后,您可以解析字典并保存正确的属性:

 NSManagedObject *photoObject = [NSEntityDescription insertNewObjectForEntityForName:@"Photo"
                                                              inManagedObjectContext:context];    
 [photoObject setPhotographer:[myDictionary objectForKey:@"photographer"]];
 and so on...

无论您的 XML 数据结构多么复杂,如果您可以设置一个很好的实体结构,那么在 CoreData 中将其全部简化是相当容易的。如果您花时间创建实体而不是仅仅将整个字典转储到单个字段中,那么您还会更轻松地进行查询。

Why don't you just create an entity with all the data from your NSDictionary and then just parse through it.

Check this out for some CoreData code snippets. You can simply create a few entities to store the info of your dictionaries. You can then parse through your dictionary and save the proper attributes:

 NSManagedObject *photoObject = [NSEntityDescription insertNewObjectForEntityForName:@"Photo"
                                                              inManagedObjectContext:context];    
 [photoObject setPhotographer:[myDictionary objectForKey:@"photographer"]];
 and so on...

No matter how complex you XML data structure, if you can set up a nice entity structure, it is fairly easy to simply dumb it all in CoreData. You'll also have a much easier time with queries if you take the time to create entities instead of just dumping the whole dictionary in a single field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文