iOS: Delete ALL Core Data Swift

前端 未结 23 744
爱一瞬间的悲伤
爱一瞬间的悲伤 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:55

    In Swift 3.0

     func deleteAllRecords() {
            //delete all data
            let context = appDelegate.persistentContainer.viewContext
    
            let deleteFetch = NSFetchRequest(entityName: "YourClassName")
            let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
    
            do {
                try context.execute(deleteRequest)
                try context.save()
            } catch {
                print ("There was an error")
            }
        }
    

提交回复
热议问题