Swift 尝试从核心数据对象设置UITextField文本时,在prepareForSegue中找到nil

Swift 尝试从核心数据对象设置UITextField文本时,在prepareForSegue中找到nil,swift,uitableview,core-data,nsfetchedresultscontroller,Swift,Uitableview,Core Data,Nsfetchedresultscontroller,我有一个UITableView,它使用NSFetchedResultsController为UITableView获取数据。当用户在UITableView中选择一行时,我有一个编辑模式视图幻灯片,其中有两个UITextFields供用户编辑UITableViewCell中的数据。我试图完成的是使用核心数据中UITableViewCell中的文本设置每个UITextField的文本。我这样做是为了用户不必为了进行编辑而重新键入数据 问题是,当我尝试在prepareForSegue中为每个UITex

我有一个UITableView,它使用NSFetchedResultsController为UITableView获取数据。当用户在UITableView中选择一行时,我有一个编辑模式视图幻灯片,其中有两个UITextFields供用户编辑UITableViewCell中的数据。我试图完成的是使用核心数据中UITableViewCell中的文本设置每个UITextField的文本。我这样做是为了用户不必为了进行编辑而重新键入数据

问题是,当我尝试在prepareForSegue中为每个UITextField设置文本时,我遇到了一个致命错误“在展开可选值时意外地发现了nil”。我知道数据在那里,所以我不确定为什么它会发现零。在使用NSFetchedResultsController时,我遵循stackoverflow post正确编写了prepareForSegue函数。这是我的代码:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       if segue.identifier == "edit" {
        if let inputController = segue.destinationViewController as? QAInputViewController {
            let uri = currentDeck!.objectID.URIRepresentation()
            let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
            inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
            if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
                let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                inputController.questionInput.text = data?.question
                inputController.answerInput.text = data?.answer
            }

        }

    }

}

谢谢您的帮助。

我发现我的目标视图控制器的视图在尝试为每个UITextField分配文本时尚未加载,因此发生了崩溃

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "edit" {
        if let inputController = segue.destinationViewController as? QAInputViewController {
            let uri = currentDeck!.objectID.URIRepresentation()
            let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
            inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
        if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
            let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                inputController.selectedQuestion = data!.question
                inputController.selectedAnswer = data!.answer
         }
      }
   }
}
因此,我在目标视图控制器中添加了两个字符串类型的变量,以保存prepareForSegue函数中的数据,而不是尝试将选定UITableView行中的文本直接分配给UITextField,然后在ViewWillDisplay中,我将这些变量分配给每个UITextField

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "edit" {
        if let inputController = segue.destinationViewController as? QAInputViewController {
            let uri = currentDeck!.objectID.URIRepresentation()
            let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
            inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
        if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
            let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                inputController.selectedQuestion = data!.question
                inputController.selectedAnswer = data!.answer
         }
      }
   }
}
在我的目标视图控制器中:

var selectedQuestion: String = ""
var selectedAnswer: String = ""

override func viewWillAppear(animated: Bool) {

    questionInput.text = selectedQuestion
    answerInput.text = selectedAnswer
}

我发现发生崩溃是因为在尝试为每个UITextField分配文本时,目标视图控制器的视图尚未加载

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "edit" {
        if let inputController = segue.destinationViewController as? QAInputViewController {
            let uri = currentDeck!.objectID.URIRepresentation()
            let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
            inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
        if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
            let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                inputController.selectedQuestion = data!.question
                inputController.selectedAnswer = data!.answer
         }
      }
   }
}
因此,我在目标视图控制器中添加了两个字符串类型的变量,以保存prepareForSegue函数中的数据,而不是尝试将选定UITableView行中的文本直接分配给UITextField,然后在ViewWillDisplay中,我将这些变量分配给每个UITextField

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "edit" {
        if let inputController = segue.destinationViewController as? QAInputViewController {
            let uri = currentDeck!.objectID.URIRepresentation()
            let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
            inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
        if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
            let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                inputController.selectedQuestion = data!.question
                inputController.selectedAnswer = data!.answer
         }
      }
   }
}
在我的目标视图控制器中:

var selectedQuestion: String = ""
var selectedAnswer: String = ""

override func viewWillAppear(animated: Bool) {

    questionInput.text = selectedQuestion
    answerInput.text = selectedAnswer
}

哪个变量是
nil
?您是否尝试添加一个异常断点来查找它在哪一行上崩溃?根据我检查的结果,它似乎在这些行上崩溃inputController.questionInput.text=data?.question inputController.answerInput.text=data?.Answer您确定是这些行吗,因为错误提到“展开”,除非
questionInput
answerInput
是隐式展开的选项且
nil
,否则错误可能不存在。你能用断点跟踪代码吗?单步遍历函数,看看它在哪里崩溃。所以我只在inputController.answerInput.text=data?上设置了一个断点。回答并得到了崩溃。之前的一切都还可以。我是否无法设置UITextfields的文本?基本上,我只想用TableView中选定行的核心数据对象填充它们。哪个变量是
nil
?您是否尝试添加一个异常断点来查找它在哪一行上崩溃?根据我检查的结果,它似乎在这些行上崩溃inputController.questionInput.text=data?.question inputController.answerInput.text=data?.Answer您确定是这些行吗,因为错误提到“展开”,除非
questionInput
answerInput
是隐式展开的选项且
nil
,否则错误可能不存在。你能用断点跟踪代码吗?单步遍历函数,看看它在哪里崩溃。所以我只在inputController.answerInput.text=data?上设置了一个断点。回答并得到了崩溃。之前的一切都还可以。我是否无法设置UITextfields的文本?基本上,我只想用TableView的选定行中的核心数据对象填充它们。