任何不能与字典文字一起使用

时间:2017-08-20 11:28:20

标签: ios watchkit watch-os-3

我试图在WatchKit上使用带有上下文的segue,我遇到了这个问题:

  

上下文类型' [任何]'不能与字典文字一起使用

以下是我正在使用的代码:

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

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

在segue目的地,这段代码没有产生任何错误:

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


希望有人可以帮忙!

1 个答案:

答案 0 :(得分:0)

当您尝试返回单个字典文字时,返回类型需要是一个对象数组。

您尝试覆盖的功能用于将上下文发送到基于页面的接口控制器数组。你确定,那是你想要做的吗?从你的代码来看,你应该重写函数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"]
    }
}
相关问题