[英]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.