gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:41 25 4
gpt4 key购买 nike

我有来自网站的 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)

最佳答案

代表你的评论......我会说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

关于ios - Swift:无法将类型 '__NSCFArray' 的值转换为 'NSDictionary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33123797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com