繁体   English   中英

具有相同名称的核心数据删除对象

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM