【问题标题】:Any can not be used with dictionary literal任何不能与字典文字一起使用
【发布时间】:2017-08-20 11:28:19
【问题描述】:

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

上下文类型“[Any]”不能与字典文字一起使用

这是我使用的代码:

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


希望有人能帮忙!

【问题讨论】:

    标签: ios watchkit watchos-3


    【解决方案1】:

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

    您尝试覆盖的函数用于将上下文发送到基于页面的界面控制器数组。你确定,这就是你想要做的吗?从您的代码看来,您应该覆盖函数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"]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多