我的一张桌子有以下代码。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InventoryTableCell", for: indexPath as IndexPath) as! InventoryTableViewCell
let inventory : Inventory = fetchedResultsController.object(at: indexPath as IndexPath) as! Inventory
cell.inventoryItem = inventory
cell.drawCell() //uses passed inventoryItem to draw it's self accordingly.
return cell
}
我遇到了运行时错误 cell.inventoryItem = inventory
它说线程1:EXC_BAD_ACCESS(错误:CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序)
它没有在swift 2中执行此操作,因此不确定错误是什么。我将截取我的模型和创建的类的屏幕截图。
我不知道如何解决该错误,因为这以前工作。发生了什么变化?
看来我已经解决了我的问题,当您定义项目时,您现在必须为其提供上下文,否则它将无法正常工作。
在我未在此处为我的InventoryTableViewCell显示的代码中,我错误地定义了ventureItem。
这是正确的方法。
//Set inventory item as an InventoryType within the correct context
var inventoryItem = Inventory(context: (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句