0

I was trying to use segue with context on WatchKit, and i ran into this issue:

Contextual type '[Any]' cannot be used with dictionary literal

Here is the code i was using:

override func contextsForSegue(withIdentifier segueIdentifier: String) -> [Any]? {

    if segueIdentifier == "One" {
        return ["sceneName" : "One"]
    } else if segueIdentifier == "Two" {
        return ["sceneName": "Two"]
    } else {
        return ["sceneName": "Three"]
    }
}

On the segue destination there's this code, that produces no errors:

@IBOutlet var theName: WKInterfaceLabel!

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    // Configure interface objects here.
    self.setTitle("")
    let dict = context as? NSDictionary
    if dict != nil {
        let segueContext = dict![["sceneName"]] as! String
        theName.setText(segueContext)
    }
}


Hope someone can help!

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

1 Answers1

0

The return type needs to be an array of objects, while you are trying to return a single dictionary literal.

The function you are trying to override is used to send context to an array of page based interface controllers. Are you sure, that is what you are trying to do? From you code, it seems like, you should be overriding the function contextForSegue(withIdentifier segueIdentifier: String) -> Any?.

override func contextForSegue(withIdentifier segueIdentifier: String) -> Any? {

    if segueIdentifier == "One" {
        return ["sceneName" : "One"]
    } else if segueIdentifier == "Two" {
        return ["sceneName": "Two"]
    } else {
        return ["sceneName": "Three"]
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116