Ios prepareforsgue首先返回nil
我收到Ios prepareforsgue首先返回nil,ios,swift,uitableview,Ios,Swift,Uitableview,我收到致命错误:当我从单元格中分离时,在展开可选值时意外发现nil 代码如下: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 1 { let listIngredients = recipeItem.ingredients[indexPath.row] selectedIngre
致命错误:当我从单元格中分离时,在展开可选值时意外发现nil
代码如下:
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)")
}
}
以下是我从调试器获得的信息:
selectedIngredient
nil
selectedIngredient
Optional(Ingredient {
name = Rye Whiskey;
inStock = 1;
type = IngredientType {
name = Spirit;
};
})
fatal error: unexpectedly found nil while unwrapping an Optional value
如您所见,selectedgredient
打印两次。第一次为nil,第二次为预期内容。如果我将destination.ingredientem=selectedIngredient
替换为destination.ingredientem=recipeItem.components[0]。component
序列运行正常,没有错误。您正在检查indexPath的节是否为1,如果不是,它仍将执行序列。确保您的可选单元格位于第1节(或第0节?)中,并将您的PerformsgueWithIdentifier(“showIngredientInRecipe”,发件人:self)
调用if语句以使其更安全。修复:
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)")
}
}
}
}
您的代码很容易出错,因为您可以执行segue而不考虑
部门
获取选定项的更好方法是在委托内部调用indexPathForSelectedRow()
方法
谢谢我将performsguewithidentifier(“showIngredientInRecipe”,sender:self)
移动到if语句中,但我还是先得到了nil,然后才得到了预期的结果。let svc=segue.destinationViewController
已经给了您IngredientDetailViewController
。你能告诉我它在哪一行给你零吗
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
}
}
}