当试图传递从CoreData调用的数据时,返回的错误显示线程1:EXC_BAD_指令(code=EXC_i386_INVOP,subcode=0x0)。调试器读取
致命错误:在展开可选值时意外找到nil
但是,在将变量分配给文本字段之前添加print语句时,变量将正确打印出来。我迷路了。任何帮助都将不胜感激。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let myVC = storyboard?.instantiateViewController(withIdentifier: "editInventoryItem") as! AddInventoryItemVC
          var inventoryItemName: String = ""
          var inventoryItemStock: Double = 0.0
          var inventoryItemPG: Double = 0.0
          var inventoryItemVG: Double = 0.0
          let itemName = flavors[indexPath.row]

          let appDelegate = UIApplication.shared.delegate as! AppDelegate
          let context = appDelegate.persistentContainer.viewContext
          let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Inventory")

          request.predicate = NSPredicate(format: "inventoryItemName ==%@", itemName)
          if let result = try? context.fetch(request) {
              for object in result as! [NSManagedObject] {
                  inventoryItemName = object.value(forKey: "inventoryItemName") as! String
                  inventoryItemStock = object.value(forKey: "inventoryItemStock") as! Double
                  inventoryItemPG = object.value(forKey: "inventoryItemPGPercent") as! Double
                  inventoryItemVG = object.value(forKey: "inventoryItemVGPercent") as! Double
              }
          } else {

          }

          myVC.itemName.text = inventoryItemName
          myVC.itemStock.text = "\(inventoryItemStock)"
          myVC.itemPG.text = "\(inventoryItemPG)"
          myVC.itemVG.text = "\(inventoryItemVG)"

          performSegue(withIdentifier: "editInventoryItem", sender: "")
      }

编辑:
在应用了Rinki的建议之后,Xcode给出了以下更正,并且在运行时仍然返回相同的错误。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let itemName = flavors[indexPath.row]

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Inventory")

    request.predicate = NSPredicate(format: "inventoryItemName ==%@", itemName)
    if let result = try? context.fetch(request) {
        for object in result as! [NSManagedObject] {
            inventoryItemName = object.value(forKey: "inventoryItemName") as! String
            inventoryItemStock = object.value(forKey: "inventoryItemStock") as! Double
            inventoryItemPG = object.value(forKey: "inventoryItemPGPercent") as! Double
            inventoryItemVG = object.value(forKey: "inventoryItemVGPercent") as! Double
        }
    } else {

    }
    performSegue(withIdentifier: "editInventoryItem", sender: "")
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "editInventoryItem" {
        let myVC = segue.destination as! AddInventoryItemVC
        myVC.itemName.text = inventoryItemName
        myVC.itemStock.text = "\(inventoryItemStock)"
        myVC.itemPG.text = "\(inventoryItemPG)"
        myVC.itemVG.text = "\(inventoryItemVG)"
    }
}

更新:经过一些打印,现在可以理解为什么错误会返回。prepareForSegue()didSelectRow之前被调用,因此当前值为零,但我不知道如何在调用didSelectRow之前调用prepareForSegue()
编辑:在做了一些研究并实现了一个Singleton之后,我已经解决了这个问题。我在Singleton中添加了一个identifier变量,在调用didSelectRow时将该变量更改为editInventoryItem。在第二个VC中,我设置了一个简单的if语句,它比较identifiereditInventoryItem并填充有问题的文本字段。
谢谢大家的帮助。一个错误,可能有5000000个!:天

最佳答案

您的问题与核心数据无关,而是您对segue的使用。performSegue方法是从情节提要创建一个新的viewController。从myVC创建的instantiateViewController在函数结束时被丢弃。而是使用prepareForSegue:sender:将值添加到viewController。

10-06 09:58