Search code examples
iosswiftswift3segueuistoryboardsegue

perform segue and send tuple as sender


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

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

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

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

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

Has anyone else experienced this?


Solution

  • 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.

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