Swift:无法将'__NSCFArray'类型的值转换为'NSDictionary'

时间:2015-10-14 11:12:50

标签: ios swift dictionary swift2

我有来自网站的JSON数据。我制作了主词典,我可以解析除一个子词典之外的所有数据。我收到错误“Swift:无法将类型'__NSCFArray'的值转换为'NSDictionary'”

我的数据示例。我无法解析“weather”,但我可以解析所有其他词典,例如"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)

1 个答案:

答案 0 :(得分:15)

代表你的评论...我想说windDictionary是词典......

Dictionary denotes in JSON with {} and 
Array denotes with [] // In printed response you may have array with ()

所以,你的天气部分是字典数组......你必须像

那样解析它
 let weather = mainDictionary["weather"] as! [[String : AnyObject]]  // although please not use force unwrap .. either use `if let` or `guard` statement