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

问题描述

我有一个NSDictionary和一个CoreData数据库。


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



=h2_lin>解决方案

您需要将NSDictionary序列化为NSData,CoreData可以保存到NSData。

但是您将无法搜索(谓词)元素的NSDictionary。



如果我们考虑这一点,NSDictionary是数据的集合。

数据库中的表是一种数据集合。 br>
在CoreData中,最接近数据集合的是NSManagedObject。



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


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 ?

解决方案

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.

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

10-23 00:37