2014-11-07 4 views
0
//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 
     } 
    } 

Как удалить все строки в Core Data с именем поля "itemName"?Ключевые данные Удалить объекты с таким же именем

+0

Я не знаю, код, который я должен выполнить, чтобы удалить все объекты с именем = itemName ... –

ответ

1

Создайте 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) 
} 
+0

Огромное спасибо :) –

+0

Рад помочь :-) – zisoft

+0

Я помню, что нам все еще приходилось использовать% @ с строкой форматированного формата, для Objective-C и Swift –

1

попробовать этот

if let index = find(items, items.objectAtIndex(indexPath.row)) 

       { 
       items.removeAtIndex(index) 

       }