Swift中的字典数组

sol*_*eil 2 arrays json dictionary swift

似乎找不到合适的语法.试图从json文件中获取一系列dicts:

let books:Array = jsonDict["books"] as Array
//Cannot convert the expression's type Array<$T4> to type StringLiteralConvertible

let books:Array = jsonDict["books"] as Array<Dictionary>
//Reference to generic type "Dicionary" requires arguments in <...>
Run Code Online (Sandbox Code Playgroud)

json看起来像这样:

{
  "id": "1",
  "title": "title one",
  "books": [
    {
      "id": "1",
      "title": "book title one"
    },
    {
      "id": "2",
      "title": "book title two"
    }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*mon 6

指定数组时,您还必须具体并列出数组中的内容.你还需要

您可能正在寻找的是:

let books = jsonDict["books"]
Run Code Online (Sandbox Code Playgroud)

编译器应该能够推断出那里有什么.如果没有,你可以使用:

let books = jsonDict["books"] as Array<Dictionary<String, String>>
Run Code Online (Sandbox Code Playgroud)

具有类型转换书籍的类型说明符是不必要的.