Swift:无法将types'__NSCFArray'的值转换为'NSDictionary'
我有来自网站的JSON数据。 我做了主要的字典,我可以parsing除了一个子字典以外的每一个数据。 我得到错误“Swift:无法将types'__NSCFArray'的值转换为'NSDictionary'”
我的数据的这个例子。 我不能parsing“天气”,但我可以parsing所有其他字典,如"wind"
。
["name": Mountain View, "id": 5375480, "weather": ( { description = "sky is clear"; icon = 01n; id = 800; main = Clear; } ), "base": cmc stations, "wind": { deg = "129.502"; speed = "1.41";
代码片段
let windDictionary = mainDictionary["wind"] as! [String : AnyObject let speed = windDictionary["speed"] as! Double print(speed) let weather = mainDictionary["weather"] as! [String : AnyObject] print(weather)
代表你的评论…我会说windDictionary是字典…
Dictionary denotes in JSON with {} and Array denotes with [] // In printed response you may have array with ()
所以,你的天气部分是字典数组…你必须像parsing它
let weather = mainDictionary["weather"] as! [[String : AnyObject]] // although please not use force unwrap .. either use `if let` or `guard` statement