gpt4 book ai didi

ios - 无法使用类型为 '[[String : AnyObject]]' 的索引下标类型为 'String' 的值

转载 作者:行者123 更新时间:2023-11-28 11:00:48 26 4
gpt4 key购买 nike

我正在尝试获取特定位置的温度,但在尝试从 JSON 中提取信息时我会发现此错误:

Cannot subscript a value of type '[[String : AnyObject]]' with an index of type 'String'

代码:

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]

if let mainDictionary = json["main"] as? [[String : AnyObject]]
{

if let temp = mainDictionary["temp"] as! String
{
print(temp)

}
}

错误出现在行:

if let temp = mainDictionary["temp"] as! String

我已经尝试了在 slack 上找到的多个“解决方案”,但似乎都没有用...

提前致谢!

最佳答案

您将类型转换为:

[[String: AnyObject]]

当你应该像这样转换它时:

[String: AnyObject]

当您希望将其转换为包含键作为强键和任何对象作为值的字典数组时,您这样做的方式是将类型转换为包含字典的数组数组。

希望这对您有所帮助:)

关于ios - 无法使用类型为 '[[String : AnyObject]]' 的索引下标类型为 'String' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40626034/

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