iOS:删除所有核心数据Swift [英] iOS: Delete ALL Core Data Swift

查看:124
本文介绍了iOS:删除所有核心数据Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何删除swift中的所有核心数据有点困惑。我创建了一个 IBAction 链接的按钮。点击按钮我有以下:

I am a little confused as to how to delete all core data in swift. I have created a button with an IBAction linked. On the click of the button I have the following:

let appDel: foodforteethAppDelegate = UIApplication.sharedApplication().delegate as foodforteethAppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext

然后我弄乱了各种方法尝试并删除所有的核心数据内容,但我似乎无法得到它的工作。我使用removeAll从存储的数组中删除,但仍然不能从核心数据删除。我假设我需要一些类型的for循环,但不确定如何去从请求。

Then I've messed around with various methods to try and delete all the core data content but I can't seem to get it to work. I've used removeAll to delete from a stored array but still can't delete from the core data. I assume I need some type of for loop but unsure how to go about making it from the request.

我已尝试应用删除单行的基本原理

I have tried applying the basic principle of deleting a single row

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值,我需要循环遍历所有的价值观,我似乎无法与上下文。

However, the issue with this is that when I click a button, I don't have the indexPath value and also I need to loop through all the values which I can't seem to do with context.

推荐答案

我使用以下方法工作:

    @IBAction func btnDelTask_Click(sender: UIButton){

        let appDel: foodforteethAppDelegate = UIApplication.sharedApplication().delegate as foodforteethAppDelegate
        let context: NSManagedObjectContext = appDel.managedObjectContext  
        let request = NSFetchRequest(entityName: "Food")

        myList = context.executeFetchRequest(request, error: nil)


        if let tv = tblTasks {

            var bas: NSManagedObject!

            for bas: AnyObject in myList
            {
               context.deleteObject(bas as NSManagedObject)
            }

            myList.removeAll(keepCapacity: false)

            tv.reloadData()
            context.save(nil)
        }
    }

但是,我不确定这是否是最好的方式。我也收到一个常量推断有任何对象的错误 - 所以如果有任何解决方案,那将是很好的

However, I am unsure whether this is the best way to go about doing it. I'm also receiving a 'constant 'bas' inferred to have anyobject' error - so if there are any solutions for that then it would be great

编辑

修改为bas:AnyObject

Fixed by changing to bas: AnyObject

这篇关于iOS:删除所有核心数据Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆