Arrays Swift 3多核心数据保存

Arrays Swift 3多核心数据保存,arrays,swift,core-data,Arrays,Swift,Core Data,我需要像保存对象一样保存每个字符串,但在下面的方法中,只保存最后一个字符串 你能告诉我怎样才能保存一切吗 let appDelegate = UIApplication.shared.delegate as! AppDelegate let context = appDelegate.persistentContainer.viewContext if let doc = HTML(html: sentHTML, encodi

我需要像保存对象一样保存每个字符串,但在下面的方法中,只保存最后一个字符串

你能告诉我怎样才能保存一切吗

          let appDelegate = UIApplication.shared.delegate as! AppDelegate

            let context = appDelegate.persistentContainer.viewContext
            if let doc = HTML(html: sentHTML, encoding: .utf8) {
                print(doc.title!)
                var kCounter = 2
                var Variab = false
                let newInfo = NSEntityDescription.insertNewObject(forEntityName: "MarksTable", into: context)
                for link in doc.xpath("//td | //link") {

                    if (kCounter % 2) == 0 {

                        newInfo.setValue(link.text!, forKey: "lesson")
                    }
                    if (kCounter % 2) == 1 {

                        newInfo.setValue(link.text!, forKey: "mark")
                    }
                    kCounter += 1

                    do
                    {
                        try context.save()
                        print("saved ",link.text!)

                    }
                    catch
                    {

                    }
                }

如果要在每次迭代中创建新的托管对象,请将let newInfo移动到循环中

 for link in doc.xpath("//td | //link") {
     let newInfo = NSEntityDescription.insertNewObject(forEntityName: "MarksTable", into: context)
     ...
此外,强烈建议在循环之后保存上下文一次