Swift:PrepareForSegue,Swift Cast 失败 [英] Swift: PrepareForSegue, Swift Cast failure

查看:34
本文介绍了Swift:PrepareForSegue,Swift Cast 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

标识符正确.

我的代码:

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

异常:

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

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

怎么了?

推荐答案

更安全和更前向的方法是条件转换,或者甚至更好的基于协议,请考虑以下事项:

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

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

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

考虑协议方法:

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

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

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.

要判断你的代码哪里坏了并不容易:它通常发生在你忘记在 storyboard 中设置你的类名(在你的例子中是 FirstViewController)时,对于执行 segue 的控制器,看截图,它有被设置而不是 UIViewController 泛型类.

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.

这篇关于Swift:PrepareForSegue,Swift Cast 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆