Swift CoreData从关系中保存和获取

Swift CoreData从关系中保存和获取,swift,core-data,Swift,Core Data,我的swift应用程序中有coredata。我能够获取两个textfields.text以保存到核心数据并获取它,但这两个属性是1个实体的一部分。我还有一些其他实体,它们都建立了关系 当我保存到核心数据时,会出现两个字符串,但关系字符串为零,应用程序崩溃 这是我的一些设置 //标记:NSFetchedResultsControllerDelegate extension SavedPalauttesViewController: NSFetchedResultsControllerDelega

我的swift应用程序中有coredata。我能够获取两个textfields.text以保存到核心数据并获取它,但这两个属性是1个实体的一部分。我还有一些其他实体,它们都建立了关系

当我保存到核心数据时,会出现两个字符串,但关系字符串为零,应用程序崩溃

这是我的一些设置

//标记:NSFetchedResultsControllerDelegate

extension SavedPalauttesViewController: NSFetchedResultsControllerDelegate {

  func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {


    tableView.beginUpdates()
  }

  func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {


    tableView.endUpdates()
  }



  func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {


    switch(type) {

    case .insert:
      if let indexPath = newIndexPath {
        tableView.insertRows(at: [indexPath], with: .fade)
      }
      break

    case .delete:
      if let indexPath = indexPath {
        tableView.deleteRows(at: [indexPath], with: .fade)
      }
      break

    case.update:
      if let indexPath = indexPath{
        let cell = tableView.cellForRow(at: indexPath) as! LocalPalautteTableViewCell
        configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
      }
      break

    case .move:
      if let indexPath = indexPath {
        tableView.deleteRows(at: [indexPath], with: .fade)
      }
      if let indexPath = newIndexPath {
        tableView.insertRows(at: [indexPath], with: .fade)
      }
      break

    }
  }

  func attemptFetch() {

    let fetchRequest: NSFetchRequest<Palautte> = Palautte.fetchRequest()

    let nameSort = NSSortDescriptor(key: "name", ascending: false)
    fetchRequest.sortDescriptors = [nameSort]

    let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

    controller.delegate = self

    self.controller = controller

    do {

      try controller.performFetch()

    } catch {

      let error = error as NSError
      print("\(error)")

    }




  }

}
但在第二个vc上,当我试图为它获取价值时,我崩溃了

let backRed = palautte.toBackgroundColor?.redValue ?? ""
我做错了什么?

当应用程序崩溃时,你在Xcode中看到了什么错误消息?你需要创建一个
BackgrondColor
对象,设置它的
redValue
属性,然后将其分配给
palatte
对象上的
toBackgroundColor
关系。
let backRed = palautte.toBackgroundColor?.redValue ?? ""