将 NSDictionary 插入 CoreData [英] insert NSDictionary into CoreData

查看:14
本文介绍了将 NSDictionary 插入 CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSDictionary 和一个 CoreData 数据库.我想将 NSDictionary 插入到数据库中.

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

  1. 我该怎么做(如果可能,代码片段)?

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

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

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

推荐答案

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

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.

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

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.

所以我的建议是创建一个 NSManagedObject 子类来保存您在 NSDictionary 中的信息.key 将是属性,值将是该属性的 value.并且您将能够根据该 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.

这篇关于将 NSDictionary 插入 CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆