📜  在一个核心数据记录中存储多个项目 - Swift 代码示例

📅  最后修改于: 2022-03-11 15:01:00.294000             🧑  作者: Mango

代码示例1
func insertItems(coreDataTable : String, records: NSArray) -> Bool {
        let error: NSError?
        for(item in records as NSDictionary!) {
            let entity = NSEntityDescription.entityForName(coreDataTable, inManagedObjectContext: self.managedContext)
            var record = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext) as Record!
            record.name = item["name"]
            if (!self.mainObjectContext.save()) {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                //abort();
                return false
            }
        }
    return true
}