Swift:数组内的字典

Upv*_*ote 1 swift swift-dictionary swift-array

数据:

[
    { firstName: "Foo", lastName: "Bar" },
    { firstName: "John", lastName: "Doe" }
]
Run Code Online (Sandbox Code Playgroud)

如何使用 swift 数组和字典拥有这种结构?该数据显示数组内的字典,对吧?所以我建议:

var persons:Array = [Dictionary<String, String>()]
Run Code Online (Sandbox Code Playgroud)

但这给了我错误:

Cannot convert the expressions type () to type Array<T>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ant*_*nio 5

正确的做法是:

var persons = [Dictionary<String, String>]()
Run Code Online (Sandbox Code Playgroud)

这相当于:

var persons = [[String : String]]()
Run Code Online (Sandbox Code Playgroud)

相反,您的代码所做的是创建一个填充有 实例的数组Dictionary<String, String>,而我认为您想要一个包含 type 元素的数组的空实例Dictionary<String, String>