我想更新我的核心数据实体,而我的代码是:
let ff: Cart!
let indexPath = self.tableView?.indexPathForSelectedRow
ff = self.cart[indexPath!.row]
let app = UIApplication.sharedApplication().delegate as! AppDelegate
let context = app.managedObjectContext
let entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: context)
let cartt = Cart(entity: entity!, insertIntoManagedObjectContext: context)
cartt.name = ff.name
cartt.price = Double(ff.price!)
cartt.rest_id = ff.rest_id
cartt.longg = ff.longg
cartt.latt = ff.latt
cartt.item_id = ff.item_id
cartt.restName = ff.restName
cartt.cookTime = ff.cookTime
ff.setValue(Int(txt.text!), forKey: "quantity")
do{
try context.save()
self.viewDidLoad()
print(cartt.longg)
print(cartt.delivery)
print("saved")
}catch{
print("could not save")
}
元素在核心数据中正在更新,但是它也在tableView
空数据的基础上向我添加了新行。
如果要更新quantity
当前对象,请ff
尝试以下行。
let ff: Cart!
let indexPath = self.tableView?.indexPathForSelectedRow
ff = self.cart[indexPath!.row]
let app = UIApplication.sharedApplication().delegate as! AppDelegate
let context = app.managedObjectContext
ff.setValue(Int(txt.text!), forKey: "quantity")
do{
try context.save()
self.viewDidLoad() // why you call this line here ?
print(cartt.longg)
print(cartt.delivery)
print("saved")
}catch{
print("could not save")
}
你为什么打来电话self.viewDidLoad()
?
在您的下一个代码中,您正在创建一个新项目,而不仅仅是更新当前项目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句