Niall
Niall

Reputation: 1621

Prepare for segue: Cannot convert value of type 'UIViewController' to specified type 'SecondViewController'

I know I must be missing something obvious here, but I can't seem to reference a destination View Controller by it's actual type. I've created a new project from scratch to test this, following these steps:

Which results in an error of Cannot convert value of type 'UIViewController' to specified type 'SecondViewController'. I've tried everything I can think of, trying all the segue types etc, but am out of ideas. I know the segue itself is working as if I comment out that code it does indeed call the second View Controller (I even added a label to the designation just to be certain).

I'm new to Swift and Storyboard so it's probably something simple I'm missing here, but any help would be greatly appreciated!

Upvotes: 3

Views: 2888

Answers (2)

David Douglas
David Douglas

Reputation: 10503

In Swift you can use guard statement to unwrap the segue.destinationViewController cast

guard let destVC : SecondViewController = segue.destinationViewController as? SecondViewController else {
    return
}
destVC.id = "test"

Or use a conditional check for specific type of UIViewController where the value is not nil

if let destVC : SecondViewController? = segue.destinationViewController as? SecondViewController where destVC != nil {
    destVC?.id = "test"
    return
}

Upvotes: 0

Moriya
Moriya

Reputation: 7906

You should be able to set the value like this.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if let vc: SecondViewController = segue.destinationViewController as? SecondViewController {
        vc.id = "test"
    }
}

This will be safer as well if you add other segues.

Another way to do this would be to force cast the controller with

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    let vc: SecondViewController = segue.destinationViewController as! SecondViewController
    vc.id = "test"
}

This code should compile but it would crash if called with the wrong destinationViewController as opposed to the if let option which just wouldn't set the id value of the destination controller if it's not the intended class.

Upvotes: 5

Related Questions