Search code examples
iosswiftsegue

Could not cast value of type 'TableViewController' to 'TableViewController'


I'm trying to pass Realm data from Custom cell to Detail TableView Controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "viewTask" {
        let nextViewController = segue.destinationViewController as!  TaskViewTableViewController
        let indexPath : NSIndexPath = self.tableView?.indexPathForSelectedRow as NSIndexPath!
        let task = tasks![indexPath.row]
        nextViewController.name = task.name
        nextViewController.notes = task.notes
        nextViewController.date = task.date
        nextViewController.timeLength = task.timeLength
    }
}

I'm getting error

Could not cast value of type 'Taskio.TaskListTableViewController' (0x10001f028) to 'Taskio.TaskViewTableViewController' (0x10001f280).

Also tried

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "viewTask" {
            let nav = segue.destinationViewController as! UINavigationController
            let nextViewController = segue.destinationViewController as!  TaskViewTableViewController
            let indexPath : NSIndexPath = self.tableView?.indexPathForSelectedRow as NSIndexPath!
            let task = tasks![indexPath.row]
            nextViewController.name = task.name
            nextViewController.notes = task.notes
            nextViewController.date = task.date
            nextViewController.timeLength = task.timeLength
        }
    }

and also

let nextViewController :  TaskViewTableViewController = segue.destinationViewController as!  TaskViewTableViewController

EDIT

Forgot to put class name into storyboard of tableViewController. It's working


Solution

  • It seems that your destinationViewController is not of the type that you are expecting. You will need to check the segue to make sure it's pointing to the correct controller. If it is, you'll need to revisit your controller class hierarchy.

    Are TaskListTableViewController and TaskViewTableViewController part of the same class hierarchy? Per the Swift docs on typecasting, you can only downcast from TaskListTableViewController to TaskViewTableViewController if your List view is a subclass of your Task view. It is not enough that both are simply subclasses of UITableViewController.

    In other words, if your class hierarchy looks like this, you will not be able to downcast from the List view to the Task view:

                          ----> TaskListTableViewController
                         /
    UITableViewController
                         \
                          ----> TaskViewTableViewController
    

    But if your hierarchy looks like this, you will be able to downcast from one to the other:

    UITableViewController -> TaskListTableViewController -> TaskViewTableViewController
    

    Regarding your second attempt at casting to UINavigationController, the navigation controller is not related to UITableViewController at all, so it cannot be type casted to that class either.

    You can check out the inheritance hierarchy for UINavigationController and UITableViewController. One needs to be a subclass of the other for any type casting to work. You'll see that is not the case.