gpt4 book ai didi

ios - Xcode 继续 "Could not cast value of type"

转载 作者:行者123 更新时间:2023-11-28 12:30:24 30 4
gpt4 key购买 nike

我有转至两个不同的 ViewControllers,Detail 和 Search。转到 Detail 的 segues 工作正常,但是转到 Search 的 segues 使应用程序崩溃。我花了两个小时阅读类似的问题,但似乎都没有相同的问题:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

let detailVC = segue.destination as! DetailViewController

if segue.identifier == "newDocSegue" {
// Create a new document and pass it to the detail view.
detailVC.doc = Document()
detailVC.isAddAction = true
}

if segue.identifier == "editDocSegue" {
// Load the existing document into the detail view, for editing.
let indexPath = tableView.indexPath(for: sender as! UITableViewCell)!
detailVC.doc = Document[indexPath.row]
detailVC.isAddAction = false
}
else if segue.identifier == "searchSegue" {
shouldPerformSegue(withIdentifier: "searchSegue", sender: Any?.self)
}

}

最佳答案

你的问题是

让 detailVC = segue.destination as!细节 View Controller

行。这将尝试将任何 VC(包括您的搜索 VC)转换为 DetailViewController。试试这个:

let detailVC : UIViewController

if segue.identifier == "newDocSegue" {
// Create a new document and pass it to the detail view.
detailVC = segue.destination as! DetailViewController
detailVC.doc = Document()
detailVC.isAddAction = true
}

if segue.identifier == "editDocSegue" {
// Load the existing document into the detail view, for editing.
detailVC = segue.destination as! ??? // not sure what type you want here
let indexPath = tableView.indexPath(for: sender as! UITableViewCell)!
detailVC.doc = Document[indexPath.row]
detailVC.isAddAction = false
}
else if segue.identifier == "searchSegue" {
detailVC = segue.destination as! SearchViewController
shouldPerformSegue(withIdentifier: "searchSegue", sender: Any?.self)
}

关于ios - Xcode 继续 "Could not cast value of type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42198254/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com