【发布时间】:2016-12-18 11:37:34
【问题描述】:
我想将一个包含 28 个条目的数组存储到我的 coreData。有没有办法做到这一点? 我用这段代码试过了,但看起来这段代码只是重写了值。
let appDelegate =
UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "PillTook",
in:managedContext)
let value = NSManagedObject(entity: entity!,
insertInto: managedContext)
for var index in 0...27 {
value.setValue(false, forKey: "took")
do {
try managedContext.save()
pillTook.append(value)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
【问题讨论】: