tableview reloadData 在解包可选值时崩溃意外发现 nil
tableview reloadData crashes unexpected found nil while unwrapping optional value
我无法理解为什么 reloadData() 行因以下错误而崩溃 'unexpected found nil while unwrapping optional value'(另外,为什么它是可选的?)。基本上,当用户点击一个按钮时,它会触发一个 API 请求,显示第二个 VC(recipesVC) 并且当从 API 检索数据时,在 receivedRecipes 方法中( previous VC) 我想从食谱中重新加载数据VC (currentVC)
由于我的数据已正确传递给食谱 VC,我不明白为什么 reloadData() 不能在相同的 VC 上工作。你能给我一点帮助吗?
谢谢。
override func viewDidLoad() {
super.viewDidLoad()
let recipes = Notification.Name(rawValue: "gotRecipes")
NotificationCenter.default.addObserver(self, selector: #selector(receivedRecipes),name: recipes, object: nil)
}
@objc func receivedRecipes() {
let recipesVC = storyboard?.instantiateViewController(withIdentifier: "recipesList") as! RecipesViewController
recipesVC.recipesList = request.recipeDetails
recipesVC.recipes.reloadData()
}
为您的数据添加验证:
func receivedRecipes() {
guard let details = request.recipeDetails else { return }
let recipesVC = storyboard?.instantiateViewController(withIdentifier: "recipesList") as! RecipesViewController
recipesVC.recipesList = details
recipesVC.recipes.reloadData()
}
下一个提示,您可以更新您的通知并从 notification.object
获取食谱,但之前您应该将它们粘贴到:
let parsedRecipes = Recipes()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "gotRecipes"), object: parsedRecipes, userInfo: nil)
初始化后也显示 recipesVC
:
self.present(...)
或 self.navigationController?.show(...)
@objc func receivedRecipes() {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "recipes"), object: nil, userInfo: ["data":request.recipeDetails])
}
在你目前的VC中
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(receivedRecipes(notification:)),name: NSNotification.Name(rawValue: "recipes"), object: nil)
}
@objc func receivedRecipes(notification: Notification) {
recipesList = notification.userInfo
recipes.reloadData()
}
我无法理解为什么 reloadData() 行因以下错误而崩溃 'unexpected found nil while unwrapping optional value'(另外,为什么它是可选的?)。基本上,当用户点击一个按钮时,它会触发一个 API 请求,显示第二个 VC(recipesVC) 并且当从 API 检索数据时,在 receivedRecipes 方法中( previous VC) 我想从食谱中重新加载数据VC (currentVC) 由于我的数据已正确传递给食谱 VC,我不明白为什么 reloadData() 不能在相同的 VC 上工作。你能给我一点帮助吗? 谢谢。
override func viewDidLoad() {
super.viewDidLoad()
let recipes = Notification.Name(rawValue: "gotRecipes")
NotificationCenter.default.addObserver(self, selector: #selector(receivedRecipes),name: recipes, object: nil)
}
@objc func receivedRecipes() {
let recipesVC = storyboard?.instantiateViewController(withIdentifier: "recipesList") as! RecipesViewController
recipesVC.recipesList = request.recipeDetails
recipesVC.recipes.reloadData()
}
为您的数据添加验证:
func receivedRecipes() {
guard let details = request.recipeDetails else { return }
let recipesVC = storyboard?.instantiateViewController(withIdentifier: "recipesList") as! RecipesViewController
recipesVC.recipesList = details
recipesVC.recipes.reloadData()
}
下一个提示,您可以更新您的通知并从 notification.object
获取食谱,但之前您应该将它们粘贴到:
let parsedRecipes = Recipes()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "gotRecipes"), object: parsedRecipes, userInfo: nil)
初始化后也显示 recipesVC
:
self.present(...)
或 self.navigationController?.show(...)
@objc func receivedRecipes() {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "recipes"), object: nil, userInfo: ["data":request.recipeDetails])
}
在你目前的VC中
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(receivedRecipes(notification:)),name: NSNotification.Name(rawValue: "recipes"), object: nil)
}
@objc func receivedRecipes(notification: Notification) {
recipesList = notification.userInfo
recipes.reloadData()
}