Search code examples
swiftxcodeseguesigabrt

SIGABRT when casting one TableViewController to another


This is from the 'Everyone Can Code' eBook from Apple. The variable names are changed for my personal uses.

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)


}

This is the segue function. relatively simple

@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)
            }
        }
    }

There is a SIGABRT on the let sourceVC = segue.destination as! AddAccountTVC line, saying that it

Could not cast value of type 'Password_Alcatraz_2.AccountsTVC' (0x1019d6468) to 'Password_Alcatraz_2.AddAccountTVC' (0x1019d61b0).

Those two are different table view controllers. I am new to swift, so I do not understand exactly why it will not let me pass data between them. Any help appreciated.


Solution

  • Exactly as @Vacawama states - go to your storyboard and look at the ViewController you are GOING TO - you have it listed as a AccountsTVC when in your code you have that listed as a AddAccountTVC

    let sourceVC = segue.destination as! AddAccountTVC

    so one of them is wrong. Just adjust and you are good