循环[[String:Any]]

时间:2016-10-09 19:18:39

标签: ios json swift swift3

let posts = json["posts"] as? [[String: Any]] ?? []
print(posts)

我得到了

[["id": 1, "title": title 1], ["id": 2, "title": title 2], ["id": 3, "title": title 3], ["id": 4, "title": title 4], ["id": 5, "title": title 5]]

如何循环使用此值来获取每个idtitle的值?

1 个答案:

答案 0 :(得分:3)

使用postsfor-loop进行迭代,并确保您的值为guard

for post in posts{
    guard let id = post["id"] as? Int,
          let title = post["title"] as? String else { continue }
}