[英]Core Data Delete Objects with the same name
//taken from Core Data
var items:NSMutableArray = ["Item 1","Item 2","Item 1","Item 3"]
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == .Delete){
let itemName = items.objectAtIndex(indexPath.row) as String
//delete all rows in core data with this item name
}
}
如何刪除核心數據中具有字段名稱“ itemName”的所有行?
創建一個NSFetchRequest
並使用您的商品名稱設置一個NSPredicate
。 執行此提取請求將為您提供帶有該謂詞的所有NSManagedObjects
的數組。
然后遍歷該數組並調用NSManagedObjectContext
的方法deleteObject(object)
let fetchRequest = NSFetchRequest(entityName: "yourEntityName")
fetchRequest.includesSubentities = false
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"name == '\(itemName)'")
var error: NSError?
// moc is your NSManagedObjectContext here
items = moc.executeFetchRequest(fetchRequest, error: &error)!
for item in items {
moc.deleteObject(item)
}
嘗試這個
if let index = find(items, items.objectAtIndex(indexPath.row))
{
items.removeAtIndex(index)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.