Swift 为什么我能';t删除核心数据表中的数据

Swift 为什么我能';t删除核心数据表中的数据,swift,core-data,Swift,Core Data,我正在做一个示例应用程序:一个添加按钮添加联系人的电话号码;当我滑动该行时,它将显示delete,我可以删除该行中的数据,同时,coredata中的数据也消失了 我编写了以下代码:添加函数可以很好地处理tableview和核心数据。但是对于delete,我可以删除tableview中的数据,但是看起来数据并没有从核心数据中删除。但是,它没有给我一个错误消息。我只是不知道出了什么问题,为什么我不能在核心数据中删除它。请帮助。多谢各位 func tableView(tableView: UITa

我正在做一个示例应用程序:一个添加按钮添加联系人的电话号码;当我滑动该行时,它将显示delete,我可以删除该行中的数据,同时,coredata中的数据也消失了

我编写了以下代码:添加函数可以很好地处理tableview和核心数据。但是对于delete,我可以删除tableview中的数据,但是看起来数据并没有从核心数据中删除。但是,它没有给我一个错误消息。我只是不知道出了什么问题,为什么我不能在核心数据中删除它。请帮助。多谢各位

  func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let managedContext = appDelegate.managedObjectContext


        if (editingStyle == UITableViewCellEditingStyle.Delete){

             let item = items[indexPath.row]


                managedContext.deleteObject(item)


                items.removeAtIndex(indexPath.row)

            tableView.reloadData()

        }
    }





    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // fetching the contacts from the core data

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let managedContext = appDelegate.managedObjectContext

        let fetchRequest = NSFetchRequest(entityName: "Contact")

        do {

          let results = try managedContext.executeFetchRequest(fetchRequest)
            items = results as! [NSManagedObject]


        } catch {
            print("there is an error")

        }

    }

您必须保存您的
managedContext
,以便您的更改(删除)得以反映

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext

    if (editingStyle == UITableViewCellEditingStyle.Delete){
         let item = items[indexPath.row]
         managedContext.deleteObject(item)

         do {
             try managedContext.save()
         } catch let error as NSError {
             print("\(error)") 
         }

         items.removeAtIndex(indexPath.row)
         tableView.reloadData()
    }
}