【问题标题】:Swift 2 to 3 Migration Core Data NSManagedObject ClassSwift 2 到 3 迁移核心数据 NSManagedObject 类
【发布时间】:2017-02-14 19:53:41
【问题描述】:

我的一张表有以下代码。

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 上遇到运行时错误

上面写着 Thread 1: EXC_BAD_ACCESS (error: CoreData: error: Failed to call specified initializer on NSManagedObject class )

它在 swift 2 中没有这样做,所以不确定错误是什么。我将截取我的模型和创建的类。

我不知道如何修复该错误,因为这之前有效。发生了什么变化?

【问题讨论】:

  • 不知道你是否在“InventoryTableViewCell”类中初始化了变量“inventoryItem”。如果您已启动此操作,请将其更改为可选变量。它也可能导致同样的问题。

标签: swift core-data swift2 swift3 ios10


【解决方案1】:

看起来我解决了我的问题,当您定义项目时,您现在必须为其提供上下文,否则它将不起作用。

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

这是正确的做法。

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

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 2017-02-06
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2018-03-01
    • 1970-01-01
    • 2014-04-09
    相关资源
    最近更新 更多