2016-10-06 95 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)