将一个 TableViewController 转换为另一个时的 SIGABRT
SIGABRT when casting one TableViewController to another
这是来自 Apple 的 'Everyone Can Code' 电子书。变量名称已更改以供我个人使用。
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)
}
}
}
let sourceVC = segue.destination as! AddAccountTVC
行有个SIGABRT
,说是
Could not cast value of type 'Password_Alcatraz_2.AccountsTVC'
(0x1019d6468) to 'Password_Alcatraz_2.AddAccountTVC' (0x1019d61b0).
这两个是不同的 table 视图控制器。我是 swift 的新手,所以我不明白为什么它不允许我在它们之间传递数据。任何帮助表示赞赏。
正如@Vacawama 所述 - 转到你的情节提要并查看 ViewController 你将要进行的 - 你将其列为 AccountsTVC 而在你的代码中你将其列为 AddAccountTVC
let sourceVC = segue.destination as! AddAccountTVC
所以其中一个是错误的。只要调整就可以了
这是来自 Apple 的 'Everyone Can Code' 电子书。变量名称已更改以供我个人使用。
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)
}
}
}
let sourceVC = segue.destination as! AddAccountTVC
行有个SIGABRT
,说是
Could not cast value of type 'Password_Alcatraz_2.AccountsTVC' (0x1019d6468) to 'Password_Alcatraz_2.AddAccountTVC' (0x1019d61b0).
这两个是不同的 table 视图控制器。我是 swift 的新手,所以我不明白为什么它不允许我在它们之间传递数据。任何帮助表示赞赏。
正如@Vacawama 所述 - 转到你的情节提要并查看 ViewController 你将要进行的 - 你将其列为 AccountsTVC 而在你的代码中你将其列为 AddAccountTVC
let sourceVC = segue.destination as! AddAccountTVC
所以其中一个是错误的。只要调整就可以了