本文介绍了如何在CoreData中保存多个条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码可以正确执行。问题在于它仅保存最后一个条目( Jack Daniels,3)。
I have the following code that executes without error. The problem is it only saves the last entry ("Jack Daniels", 3). How do change this so it will save all three entries?
let employees = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
employees.setValue("John Doe", forKey: "employeename")
employees.setValue(1, forKey: "id")
employees.setValue("Jane Doe", forKey: "employeename")
employees.setValue(2, forKey: "id")
employees.setValue("Jack Daniels", forKey: "employeename")
employees.setValue(3, forKey: "id")
do {
try managedObject.save()
} catch {
print("problem saving")
}
推荐答案
let employees = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
let employees1 = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
let employees2 = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
employees.setValue("John Doe", forKey: "employeename")
employees.setValue(1, forKey: "id")
employees1.setValue("Jane Doe", forKey: "employeename")
employees1.setValue(2, forKey: "id")
employees2.setValue("Jack Daniels", forKey: "employeename")
employees2.setValue(3, forKey: "id")
do {
try managedObject.save()
} catch {
print("problem saving")
}
这篇关于如何在CoreData中保存多个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!