2016-10-09 145 views
-3

隨着遍歷[字符串:任何]

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的我怎麼能循環通過這個值?

回答

3

通過postsfor-loop迭代並確保您的值爲guard

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