【发布时间】:2014-08-30 18:31:17
【问题描述】:
我对如何快速删除所有核心数据感到有些困惑。我创建了一个带有IBAction 链接的按钮。单击按钮后,我有以下内容:
let appDel: foodforteethAppDelegate = UIApplication.sharedApplication().delegate as foodforteethAppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
然后我搞砸了各种方法来尝试删除所有核心数据内容,但我似乎无法让它工作。我已经使用 removeAll 从存储的数组中删除,但仍然无法从核心数据中删除。我假设我需要某种类型的 for 循环,但不确定如何从请求中实现它。
我尝试过应用删除单行的基本原理
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
let appDel: foodforteethAppDelegate = UIApplication.sharedApplication().delegate as foodforteethAppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
if editingStyle == UITableViewCellEditingStyle.Delete {
if let tv = tblTasks {
context.deleteObject(myList[indexPath!.row] as NSManagedObject)
myList.removeAtIndex(indexPath!.row)
tv.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
}
var error: NSError? = nil
if !context.save(&error) {
abort()
}
}
}
但是,这样做的问题是,当我单击按钮时,我没有 indexPath 值,而且我需要遍历所有我似乎无法与上下文相关的值。
【问题讨论】:
-
那是正常的objective-c。我试图在 Swift 中重新创建它,但它的工作方式不同,让我感到困惑:/
-
向我们展示一些您在 Swift 中尝试过的各种方法,也许我们可以帮助您。就 API 和如何做到这一点而言,这个问题已经回答了所有问题,您应该只在 Swift 中做同样的事情。
-
问题已经在这里以更有效的方式得到解答 - stackoverflow.com/questions/1383598/…