我对如何在swift中删除所有核心数据感到有点困惑。我创建了一个链接IBAction
的按钮。在按钮的点击,我有以下:iOS:删除所有核心数据Swift
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价值,也是我需要循环所有的值,我似乎无法处理上下文。
可能重复核心数据?](http://stackoverflow.com/questions/1077810/delete-reset-all-entries-in-core-data) – Jack
这是正常的目标-c。我试图在Swift中重新创建它,但它不是以同样的方式工作并令我困惑:/ – Prateek
那么向我们展示一些您在Swift中尝试过的各种方法,或许我们可以帮助您。就API和如何做到这一点而言,这个问题可以解决所有问题,并且只需在Swift中做同样的事情。 – Jack