访问字典数组的嵌套数组

时间:2018-08-17 10:01:19

标签: ios swift xcode

我想在表视图上显示“描述”的值,但是我无法到达“描述”。每次循环。例子

for i in item { 
   let result = i["description"]     
}

它返回类型“ Any”没有下标成员

[[["description": "Meeting", "comments": "", "projectId": 6, "hours": 0.0, "id": 52, "projectName": "A-DA Internal"], ["description": "Others", "comments": "", "projectId": 6, "hours": 0.0, "id": 53, "projectName": "A-DA Internal"], ["description": "3.1 Project Planning", "comments": "", "projectId": 5, "hours": 0.0, "id": 28, "projectName": "STARWorks ESS"], ["description": "4.1 Analysis & Design", "comments": "", "projectId": 5, "hours": 0.0, "id": 32, "projectName": "STARWorks ESS"], ["description": "4.3 Bug fixes", "comments": "", "projectId": 5, "hours": 0.0, "id": 34, "projectName": "STARWorks ESS"]]]

2 个答案:

答案 0 :(得分:0)

根据您在其上方发布的回复,其类型似乎为[[[String : Any]]]。 因此,要进入[String: Any]部分,您将不得不跳入两个层次。

//let say
let result: [[[String: Any]]] = THE RESPONSE YOU POSTED..

let item = result.flatmap {$0} //this will convert to [[String: Any]]

 //After that your loop will work as expected

 for i in item {
        debugPrint(i["description"])
 }

答案 1 :(得分:0)

您需要告诉编译器要循环遍历的项目类型为 [[String:Any]] ,只有您可以获取键说明

您可以通过以下方式进行操作:

for i in item as! [[String:Any]] { 
   let result = i["description"]     
}

希望这会有所帮助。对于任何查询,请随时发表评论。