小编典典

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

swift

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

我的数据的这个例子。我无法解析 “天气”, 但可以解析所有其他字典,例如"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)

阅读 258

收藏
2020-07-07

共1个答案

小编典典

代表您的评论…我会说windDictionary是Dictionary …

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
2020-07-07