I have the following in my viewDidAppear:
override func viewDidAppear(_ animated: Bool) {
// Get the index based off of which tableview cell was selected in the last VC
let selectedRecipeIndex = recipe.firstIndex(where: {$0.docID == passedDocID})
// Get the appropriate data based off of the index determined above
let currentRecipe = recipe[selectedRecipeIndex!]
titleLabel.text = currentRecipe.title
}
I'll sometimes get the following error on selectedRecipeIndex!: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
This error is very inconsistent. For the same index/cell, it will return a value but then if I open the app a separate time and tap the same index/cell it might return nil. It's seemingly random.
Why could the same index/cell sometimes return the value but sometimes return nil? And if it returns nil, what can I do to keep trying to call the value until it is not nil?
if let…instead of force unwrappingsuper.ViewDidAppear(animated)before doing anything in the methodrecipe. Until you tell us how that is populated and why you are so certain that it contains an element whosedocIDispassedDocID(and what that is and how it gets populated), this is all just hand-waving.