2016-09-24 156 views
0

如何刪除實體中特定屬性中的所有數據我不想清空實體中的所有屬性這裏是我現在刪除實體中的所有屬性刪除核心數據項

func deletObject(){ 
    let fetchRequest = NSFetchRequest() 
    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
    fetchRequest.includesPropertyValues = false 
    let context:NSManagedObjectContext = appDel.managedObjectContext 
    let moc = context 
    fetchRequest.entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: moc) 

    do { 
     if let results = try moc.executeFetchRequest(fetchRequest) as? [NSManagedObject] { 
      for result in results { 
       moc.deleteObject(result) 
      } 
      try moc.save() 
      self.cart = [Cart]() 
      self.tableView.reloadData() 
      totalAmouLa.text = "₦\(Int(totalHoursWorkedSum))" 
     } 
    } catch { 
     print("FAILED") 
    } 

} 

編輯 我想刪除deliveryCart實體 Image

回答

1

不能刪除從核心數據模型,Y屬性ou只能重置的值。

executeFetchRequest回報保證成功[Cart],可選的綁定將永遠不會失敗

func deletObject(){ 
    let fetchRequest = NSFetchRequest() 
    let appDel = UIApplication.sharedApplication().delegate as! AppDelegate 
    let context = appDel.managedObjectContext 
    fetchRequest.entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: context) 

    do { 
     let results = try context.executeFetchRequest(fetchRequest) as! [Cart] 
     results.forEach { $0.delivery = 0.0 } 
     try context.save() 
     self.tableView.reloadData() 
     totalAmouLa.text = "₦\(Int(totalHoursWorkedSum))" 
    } catch { 
     print("FAILED") 
    } 
} 
+0

好將它重交付 – lordxx

+0

我想提出正確的問題是,所有的價值觀有沒有辦法覆蓋在數據屬性 – lordxx