xcode Swift:上下文类型“AnyObject”不能与字典文字一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38065806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:58:51  来源:igfitidea点击:

Swift: Contextual type 'AnyObject' cannot be used with dictionary literal

arraysjsonxcodeswiftdictionary

提问by Kiley

I am having difficulty constructing this dictionary. My code looks like this:

我在构建这本词典时遇到了困难。我的代码如下所示:

var array: [String] = []
let params: [String: AnyObject] = [
    "presentation": [
            "array": array,
            "current_index": 0
    ]
]

The error shows up on the first line "presentation": [with Contextual type 'AnyObject' cannot be used with dictionary literal. I have tried rewriting the array, initializing the params then setting the values, etc. etc. and I cannot figure out this problem. Any help would be awesome!

错误显示在第一行"presentation": [上下文类型 'AnyObject' cannot be used with dictionary literal。我试过重写数组,初始化参数然后设置值等等,但我无法弄清楚这个问题。任何帮助都是极好的!

Thank you in advance!

先感谢您!

采纳答案by Davyd Geyl

Try this

尝试这个

let params: [String: [String: AnyObject]]

let params: [String: [String: AnyObject]]

And close the quotes after the current_indexkey.

并关闭current_index键后的引号。

回答by Arthi

Try this in swift 3 it works

在 swift 3 中试试这个它有效

var array: [String] = []
let params: [String: AnyObject] = [
    "presentation": [
            "array": array,
            "current_index": 0
    ] as AnyObject
]