Swift: PrepareForSegue, Swift Cast failure

I would like to select one cell in my TableViewController . The text of the cell must be passed through segue to the label of the FirstViewController. I always get the error shown below.

The identifier is correct.

My code is:

 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" } } 

An exception:

0x103f1f5dc: jne 0x103f1f5d0; swift_dynamicCastClassUnconditional + 48 0x103f1f5de: leaq 0x3364d (% rip),% rax; "Too dynamic dynamics" 0x103f1f5e5: movq% rax, 0xa456c (% rip); gCRAnnotations + 8 0x103f1f5ec: int3
0x103f1f5ed: movq% r14,% rax

What's wrong?

+3
source share
5 answers

Maybe late in the game, but was getting exactly the same problem. The problem here was that your Segue was pointing to the Tab controller, not the actual ViewController where it needed to go. I mean the second screenshot you provided to Antonio. If you fix it, it should work fine.

+2
source

A safer and more direct approach is a conditional listing or even better protocol-based listing, 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)") } } 

It certainly works. But if you have several segues and multiple viewController, this can be a big problem for changing 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 do the above work, you just need to declare your UIViewController to match the specified protocol:

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

Over time, you will be able to rename your classes, change them, replace and expand your project, and you will not have to worry about changing the links if the protocol retains its purpose.

It is not easy to say where your code is broken: this usually happens when you forget to specify the name of your class (FirstViewController in your case) in the storyboard, for the controller for which segue is executed, see screenshot, it has instead of the standard class UIViewController.

view screenshot

+2
source

I had a similar error - I believe that segue.destinationViewController here UINavigationController . You get an error because you are trying to include the UINavigationController in your FirstViewController class.

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

: let vc = segue.destinationViewController.topViewController as FirstViewController

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

+1
source

Check your storyboard, I think you are making an โ€œexactโ€ point for another viewController by mistake.

0
source

Try it,

 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.

0
source

Source: https://habr.com/ru/post/974159/


All Articles