2016-08-28 3 views
2

私はセルから離したときにfatal error: unexpectedly found nil while unwrapping an Optional valueを取得しています。ここでprepareForSegueは最初にnilを返します

コードです:あなたはselectedIngredient版画二回、見ることができるように

 selectedIngredient 
     nil 

     selectedIngredient 
     Optional(Ingredient { 
      name = Rye Whiskey; 
      inStock = 1; 
      type = IngredientType { 
       name = Spirit; 
      }; 
     }) 

     fatal error: unexpectedly found nil while unwrapping an Optional value 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.section == 1 { 
     let listIngredients = recipeItem.ingredients[indexPath.row] 
     selectedIngredient = listIngredients.ingredient 
    } 
    tableView.deselectRowAtIndexPath(indexPath, animated: false) 
    performSegueWithIdentifier("showIngredientInRecipe", sender: self) 


} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showIngredientInRecipe" { 
     let svc = segue.destinationViewController as! UINavigationController 
     let destination = svc.topViewController as! IngredientDetailViewController 

     destination.ingredientItem = selectedIngredient 


     print("selectedIngredient \n \(selectedIngredient)") 

    } 

} 

ここで私は、デバッガから何を得るのです。最初は無制限で、期待されるコンテンツで2回目です。私がdestination.ingredientItem = selectedIngredientdestination.ingredientItem = recipeItem.ingredients[0].ingredientに置き換えた場合、segueはエラーなしで正常に動作します。

+0

プレゼンテーションの進行中にを表示しようとすると、[prepareForSegueが2回呼び出される可能性があります]の重複の可能性があります(http://stackoverflow.com/questions/14940738/prepareforsegue-getting-called-twice-with-attempt-to -present-uinavigationcontr) – Dima

+0

「[致命的なエラー:任意の値をアンラッピングしている間に予期せぬエラーが発生しました」とはどういう意味ですか?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-nexpectedly -found-nil-while-unwrapping-an-optional-valu) – Cristik

答えて

0

:あなたはセクション

  • にかかわらず の選択した項目を取得するためのより良い方法をセグエを行うことができますので、

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
        if segue.identifier == "showIngredientInRecipe" { 
         if let selectedIndexPath = self.tableView.indexPathForSelectedRow { 
          if selectedIndexPath.section == 1 { 
           let listIngredients = recipeItem.ingredients[selectedIndexPath.row] 
           selectedIngredient = listIngredients.ingredient 
           let svc = segue.destinationViewController as! UINavigationController 
           let destination = svc.topViewController as! IngredientDetailViewController 
           destination.ingredientItem = selectedIngredient 
           print("selectedIngredient \n \(selectedIngredient)") 
          } 
         } 
        } 
    } 
    
  • 0

    indexPathのセクションが1であるかどうかを確認していて、そうでない場合は、segueを実行します。選択可能なセルがセクション1(またはセクション0?)にあることを確認し、performSegueWithIdentifier("showIngredientInRecipe", sender: self)呼び出しをifステートメントに移動して、より安全なものにします。固定

    +0

    ありがとうございました。私は 'performSegueWithIdentifier(" showIngredientInRecipe "、sender:self)'をif文に移動しましたが、最初はまだnil、その後は期待される結果が得られます。 – bearroast

    +0

    '' 'let svc = segue.destinationViewController'''はすでに' '' IngredientDetailViewController'''と思います。あなたはそれがあなたに何もないことを伝えることができますか? –

    0
    1. あなたのコードはエラーが発生しやすくなりますが、内部のindexPathForSelectedRow()メソッドを呼び出すことです委任

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
        if indexPath.section == 1 { 
         performSegueWithIdentifier("showIngredientInRecipe", sender: self) 
        } 
    } 
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
        if segue.identifier == "showIngredientInRecipe" { 
         //get selected item 
         let indexPath = self.tableView.indexPathForSelectedRow() { 
          let selectedIngredient = recipeItem.ingredients[indexPath.row] 
          print("selectedIngredient \n \(selectedIngredient)") 
          //segue 
          let svc = segue.destinationViewController as! UINavigationController 
          let destination = svc.topViewController as! IngredientDetailViewController 
          destination.ingredientItem = selectedIngredient 
         } 
        } 
    } 
    
    関連する問題