首页 > 解决方案 > 将一个 TableViewController 转换为另一个时的 SIGABRT

问题描述

这是来自 Apple 的“人人都能编程”电子书。变量名称已更改以供我个人使用。

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

    guard segue.identifier == "saveSegue" else {return}

    let website = websiteTextField.text ?? ""
    let email = emailTextField.text ?? ""
    let username = usernameTextField.text ?? ""
    let password = passwordTextField.text ?? ""
    account = Account(website: website, username: username, email: email, password: password)


}

这就是segue函数。比较简单

@IBAction func unwindToAccountsPage(segue: UIStoryboardSegue) {
        guard segue.identifier == "saveSegue" else {return}
        let sourceVC = segue.destination as! AddAccountTVC

        if let account = sourceVC.account {
            if let selectedIndexPath = tableView.indexPathForSelectedRow {
                accounts[selectedIndexPath.row] = account
                tableView.reloadRows(at: [selectedIndexPath], with: .none)
            } else {
                let newIndexPath = IndexPath(row: accounts.count, section: 0)
                accounts.append(account)
                tableView.insertRows(at: [newIndexPath], with: .automatic)
            }
        }
    }

有一个SIGABRTlet sourceVC = segue.destination as! AddAccountTVC行了,说是

无法将“Password_Alcatraz_2.AccountsTVC”(0x1019d6468)类型的值转换为“Password_Alcatraz_2.AddAccountTVC”(0x1019d61b0)。

这两个是不同的表视图控制器。我是 swift 新手,所以我不明白为什么它不允许我在它们之间传递数据。任何帮助表示赞赏。

标签: swiftxcodeseguesigabrt

解决方案


就像@Vacawama 所说的那样-转到您的故事板并查看您要使用的 ViewController-您将其列为 AccountsTVC,而在您的代码中您将其列为 AddAccountTVC

let sourceVC = segue.destination as! AddAccountTVC

所以其中一个是错误的。调整一下就好了


推荐阅读