2016-10-06 90 views
1

我对我的一个表有以下代码。Swift 2到3迁移核心数据NSManagedObject类

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中这样做,所以不知道错误是什么。我将截取我的模型并创建课程。

我不知道如何解决这个错误,因为这之前工作。什么改变了?

enter image description here enter image description here enter image description here

+0

我不知道你是否初始化了“InventoryTableViewCell”类中的变量“inventoryItem”。如果您已经启动了该功能,请将其更改为可选变量。这也可能导致同样的问题。 –

回答

1

貌似我解决我的问题,当你定义的项目你现在给一个上下文它或将无法正常工作。

在我没有为我的InventoryTableViewCell显示的代码上,我错误地定义了inventoryItem。

这是正确的方法。

//Set inventory item as an InventoryType within the correct context 
    var inventoryItem = Inventory(context: (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext)