iOS: Delete ALL Core Data Swift

前端 未结 23 745
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 11:20

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 follow

23条回答
  •  无人及你
    2020-12-04 11:47

    I have got it working using the following method:

    @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

    EDIT

    Fixed by changing to bas: AnyObject

提交回复
热议问题