从Coredata Swift中删除数据

前端之家收集整理的这篇文章主要介绍了从Coredata Swift中删除数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的tableViewController我有以下。而我正在尝试删除一个项目。
var myData: Array<AnyObject> = []

override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellID: NSString = "Cell"
    var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
    var data: NSManagedObject = myData[indexPath.row] as NSManagedObject
    Cell.textLabel?.text = data.valueForKeyPath("Name") as? String

    return Cell
}

然后尝试删除我有。

override func tableView(tableView: (UITableView!),commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        let cellID: NSString = "Cell"
        var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
        var data: NSManagedObject = myData[indexPath.row] as NSManagedObject
        data.delete(0)

        // Delete the row from the data source
        //tableView!.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade)


    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class,insert it into the array,and add a new row to the table view
    }
}
更新我的编码问题,执行swift和coredata中的数据删除。这个代码我结束了,工作。
override func tableView(tableView: UITableView,forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
            // remove the deleted item from the model
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
            context.deleteObject(myData[indexPath.row] as NSManagedObject)
            myData.removeAtIndex(indexPath.row)
            context.save(nil)

           //tableView.reloadData()
            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade)
        default:
            return

        }
}

编辑上面的Swift 2.2和Xcode 7.3.1

override func tableView(tableView: UITableView,forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {
    case .Delete:
        // remove the deleted item from the model
        let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context:NSManagedObjectContext = appDel.managedObjectContext
        context.deleteObject(myData[indexPath.row] )
        myData.removeAtIndex(indexPath.row)
        do {
            try context.save()
        } catch _ {
        }

        // remove the deleted item from the `UITableView`
        self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade)
    default:
        return
    }
}

还需要这两行代码来纠正。

var myData: Array<AnyObject> = []
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
原文链接:https://www.f2er.com/swift/320631.html

猜你在找的Swift相关文章