在核心数据中存储 NSManagedObject 数组的 NSMutableArray | swift 2

Store a NSMutableArray of NSManagedObject arrays in core data | swift 2

我对 swift 有点陌生,在数组方面我不是大师 我正在尝试找到一种方法,将 NSManagedObjects 数组存储到核心数据中。

这是一个例子:

let array = [["a", "b", "c",],["1", "2", "3",],["apple", "orange", "pear"]]

我是这样做的:

我将几个 NSManagedObjects 数组添加到 NSMutableArray 然后我将其存储为可转换数据。像这样。

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity_entry = NSEntityDescription.entityForName("EntityForArrays", inManagedObjectContext: managedContext)
let new_value = NSManagedObject(entity: entity_entry!, insertIntoManagedObjectContext: managedContext)
new_value.setValue(array, forKey: "attribute")

那么我是通过使用 NSMutableArray 走上正确的道路还是应该尝试一些新的东西?

另外,如果可以存储这个数组,我该如何接收它?

路径是否正确取决于您需要如何使用该数组。将它们全部放在一个可转换的属性中很方便,但是您不能使用谓词根据数组内容获取对象。

这样保存数组应该可行。要取回它,请使用 NSFetchRequest 获取对象,然后使用 valueForKey().

查找数组值