不使用dataModel的子类更新记录

时间:2017-03-25 13:11:52

标签: swift3

如何在不使用coredata的子类的情况下更新记录。 我不想要像人[index] .name = newName这样的方法,它是coreDatamodel的子类。

   func updateEntity(index:Int, newName: String){
         let appDelegate = UIApplication.shared.delegate as! AppDelegate

          let managedContext = appDelegate.persistentContainer.viewContext
          let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
    do {
        people = try managedContext.fetch(fetchRequest)
        let dic = ["name":newName]as NSDictionary
        people.remove(at: index)
        people.insert((dic as AnyObject) as! NSManagedObject, at: index)

    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }

    appDelegate.saveContext()
}

先谢谢

0 个答案:

没有答案