Swift: PrepareForSegue, Swift Cast failure

I would like to select one cell in my TableViewController. The text of the cell should be transferred via segue to FirstViewController’s label. I always get the error shown below.

The identifier is correct.

  • Error: Argument type double/string etc. does not conform to expected type “AnyObject”
  • Parse request.object.get(“KEY”) always returns undefined
  • iOS 9 UITableView separators insets (significant left margin)
  • Convert UIImage to NSData and convert back to UIImage in Swift?
  • UIView used as separator messed up my constraints
  • Cannot explicitly specialize a generic function
  • My code:

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if (segue.identifier == "BackToCalculator") {
            //let myRow = tableView.indexPathForSelectedRow().row+1
            let vc = segue.destinationViewController as FirstViewController
            vc.SelectedBundesland.text = "Test"
        }
    }
    

    Exception:

    0x103f1f5dc: jne 0x103f1f5d0 ; swift_dynamicCastClassUnconditional + 48
    0x103f1f5de: leaq 0x3364d(%rip), %rax ; “Swift dynamic cast failed”
    0x103f1f5e5: movq %rax, 0xa456c(%rip) ; gCRAnnotations + 8
    0x103f1f5ec: int3
    0x103f1f5ed: movq %r14, %rax

    What is wrong?

    5 Solutions Collect From Internet About “Swift: PrepareForSegue, Swift Cast failure”

    A much safer and forward approach is conditional cast, or even better protocol based, consider the following:

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if let vc = segue.destinationViewController as? FirstViewController {
            vc.SelectedBundesland.text = "Test"
        } else {
           print("Some other controller! \(segue.destinationViewController)")
        }
    }
    

    That certainly works. But if you have multiple segues and multiple viewController it can be quite a pain to swing information and data around.

    Consider the protocol approach:

    protocol BundeslandProtocol {
        var SelectedBundesland: String {get set}
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if var vc = segue.destinationViewController as? BundeslandProtocol {
            vc.SelectedBundesland.text = "Test"
        }
    }
    

    To make the above work you just have to declare your UIViewController to conform to said protocol:

    class AwesomeViewController: UIViewController, BundeslandProtocol {
     .... 
    }
    

    Over time you’ll be able to rename your classes, change them, replace and expand your project and you won’t have to care about changing references around, as long as the protocol will keep its purpose.

    It’s not easy to tell where your code is broken: it usually happen when you forget to set your class name (FirstViewController in your case) in the storyboard, for the controller to which the segue is executed, see screenshot, it has to be set instead of UIViewController generic class.

    see screenshot

    May be late in the game but was getting the exact same issue.
    The problem here was your Segue was pointing to the Tab controller rather than the actual ViewController where it needs to jump to. I am referring to the 2nd screenshot you have provided to Antonio. If you fix that it should work fine.

    I have had a similar error – I believe that segue.destinationViewController is here a UINavigationController. You are getting an error because you are trying to cast a UINavigationController into your FirstViewController class.

    Change this line: let vc = segue.destinationViewController as FirstViewController

    to this: let vc = segue.destinationViewController.topViewController as FirstViewController

    .topViewController is the top view controller on the navigation stack – which should now be an instance of FirstViewController

    Check your storyboard´╝îI think you make the segue “point” to another viewController by mistake.

    Try this,

    self.performSegueWithIdentifier("BackToCalculator", sender: self)
    
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    
        if (segue.identifier == "BackToCalculator") {
            //let myRow = tableView.indexPathForSelectedRow().row+1
            let vc = segue.destinationViewController as FirstViewController
            vc.SelectedBundesland.text = "Test"
    }
    

    U need to call self.performSegueWithIdentifier.