Down casting Error Question



I am hoping someone can explain the issue I am having, I am clearly missing something fundimental while learning Swift.


I have a storyboard segue, using the function:


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

        let controller = segue.destination as! ChooseItemViewController

        controller.delegate = self




So in this function, it trys to create a new instance of ‘ChooseItemViewController’, and sets the delegae for it to self (ie the current story board).



Now I am getting the error:


Could not cast value of type ‘UITableViewController’ (0x1b632d588) to ‘WellandPowerLive.ChooseItemViewController’


ChooseitemViewController is a UIViewController.

“Self” is a UITableViewController.


I read up about this error here:


Which I think is saying that you cannot downcast a UITableViewController to a UIViewController? Is that correct? It just seems that cannot be right, because i imagine you would want to do this quite often.


So I thought this was wrong and it goes on about the class hierarchy.

So I assume my two controlers are forked, rather than in a line like in the diagrams.


Therefore I need to make the UIViewController a subclass of Self?

self is already set as the chooseItemViewControllers Delegate.


is that even possible? What am I missing? Can someone explain for me?

Powered by WPeMatico

You may also like...

Comments are closed.