如何从Swift中的Core Data中删除几个不同的数据?

时间:2015-06-29 10:00:12

标签: ios xcode swift

我有switch五个对象。这是四个Core Data和一个StringNSData)。我将它们保存在UIImage中。然后加载Core Data,我想从Core Data中删除它们,我得到了不同的错误。我尝试了不同的方法,但它没有帮助我。如何删除这些数据?

UIViewController

我的var name: String! var text: String! var image: UIImage! var url: String! var data: String! func deleteAll() { var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) var managedObjectContext: NSManagedObjectContext = appDel.managedObjectContext! var coreDataName: NSManagedObject = (name as AnyObject?) as! NSManagedObject var coreDataImage: NSManagedObject = (image as AnyObject?) as! NSManagedObject var coreDataText: NSManagedObject = (text as AnyObject?) as! NSManagedObject var coreDataData: NSManagedObject = (data as AnyObject?) as! NSManagedObject var coreDataURL: NSManagedObject = (url as AnyObject?) as! NSManagedObject managedObjectContext.deleteObject(coreDataName) managedObjectContext.deleteObject(coreDataImage) managedObjectContext.deleteObject(coreDataText) managedObjectContext.deleteObject(coreDataData) managedObjectContext.deleteObject(coreDataURL) managedObjectContext.save(nil) buttonDelete.enabled = false buttonShare.enabled = false } } 文件

Core Data

1 个答案:

答案 0 :(得分:0)

我改变了我的代码,它对我有用。

 var detailObject: AnyObject? // It's data from FavoriteTableViewController

    func deleteAll() {
        var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
        var managedObjectContext: NSManagedObjectContext = appDel.managedObjectContext!

        var dataDelete = detailObject as! NSManagedObject

        managedObjectContext.deleteObject(dataDelete)
        managedObjectContext.save(nil)

        managedObjectContext.save(nil)
        buttonDelete.enabled = false
        buttonShare.enabled = false
    }