执行 segue 并将元组作为发件人发送 [英] perform segue and send tuple as sender

查看:34
本文介绍了执行 segue 并将元组作为发件人发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为发送者对象设置一个元组,在执行如下操作时:

I've been trying to set a tuple to the sender object when doing a perform segue like:

performSegue(withIdentifier: "", sender: ("hello", "hello2"))

但在 override func prepare(for segue: UIStoryboardSegue, sender: Any?) 尝试将发件人转换为 (String, String) 时,我遇到了转换异常

But inside override func prepare(for segue: UIStoryboardSegue, sender: Any?) I'm getting cast exception when trying to cast sender as (String, String)

似乎只有元组的第一个元素在 sender 中.

It seems that only the first element of the tuple is in sender.

我希望因为发件人是 Any? 我可以使用元组作为变量吗?

I'd expect since sender is of type Any? I could use a tuple as variable?

有其他人遇到过这种情况吗?

Has anyone else experienced this?

推荐答案

根据 documentation,发送者应该是发起 segue 的对象;这不是您将数据传递给目标视图控制器的地方.相反,您应该将它放在视图控制器的属性中,然后使用它在 prepareForSegue 中设置目标视图控制器的属性.

Per the documentation, the sender is supposed to be the object that is initiating the segue; this is not a place for you to stuff data to pass to the destination viewcontroller. Instead you should put it in a property in your view controller and then use it to set the destination view controller's properties in prepareForSegue.

无论如何,要回答您的问题,以下是您从 Any 回退的方式?到 (String, String) 的元组

Anyways, to answer your question, here is how you cast back from Any? to a tuple of (String, String)

let b = ("g", "f")
let a: Any? = b
if let c = a as? (String, String) {
    print (c)
}

这篇关于执行 segue 并将元组作为发件人发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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