xcode Swift:PrepareForSegue,Swift Cast 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25406309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:25:04  来源:igfitidea点击:

Swift: PrepareForSegue, Swift Cast failure

iosxcodeuitableviewswift

提问by Velocity

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.

我想在我的TableViewController. 单元格的文本应该通过 segue 传输到 FirstViewController 的标签。我总是收到如下所示的错误。

The identifier is correct.

标识符正确。

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

0x103f1f5dc: jne 0x103f1f5d0 ; swift_dynamicCastClassUnconditional + 48 0x103f1f5de: leaq 0x3364d(%rip), %rax ; “Swift 动态转换失败” 0x103f1f5e5: movq %rax, 0xa456c(%rip) ;gCRAnnotations + 8 0x103f1f5ec: int3
0x103f1f5ed: movq %r14, %rax

What is wrong?

怎么了?

回答by KD.

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.

可能在游戏后期,但遇到了完全相同的问题。这里的问题是您的 Segue 指向 Tab 控制器而不是它需要跳转到的实际 ViewController。我指的是您提供给安东尼奥的第二个屏幕截图。如果你修复它应该可以正常工作。

回答by André

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.

那当然有效。但是,如果您有多个 segue 和多个 viewController,那么四处摆动信息和数据可能会非常痛苦。

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:

要完成上述工作,您只需声明您的 UIViewController 以符合所述协议:

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.

很难判断您的代码在哪里损坏:通常发生在您忘记在情节提要中设置类名(在您的情况下为 FirstViewController)时,对于执行 segue 的控制器,请参见屏幕截图,必须对其进行设置而不是 UIViewController 泛型类。

see screenshot

看截图

回答by tonethar

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

我有一个类似的错误 - 我相信这segue.destinationViewController是一个UINavigationController. 您收到错误是因为您试图将 UINavigationController 转换为 FirstViewController 类。

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

改变这一行: let vc = segue.destinationViewController as FirstViewController

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

对此: let vc = segue.destinationViewController.topViewController as FirstViewController

.topViewControlleris the top view controller on the navigation stack - which should now be an instance of FirstViewController

.topViewController是导航堆栈上的顶部视图控制器 - 现在应该是 FirstViewController

回答by Diamond King

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.

你需要调用self.performSegueWithIdentifier。

回答by li_shengdm

Check your storyboard,I think you make the segue "point" to another viewController by mistake.

检查你的故事板,我认为你错误地将segue“指向”了另一个viewController。