gpt4 book ai didi

json - Swift Dark Sky API 天气应用类型 'Any' 没有下标成员

转载 作者:行者123 更新时间:2023-11-28 14:08:15 25 4
gpt4 key购买 nike

<分区>

我目前正在尝试完成有关读取 JSON 文件的在线教程,教程文件让我们输入以下代码

override func viewDidLoad() {
super.viewDidLoad()

if let url = NSURL(string: "https://api.forecast.io/forecast/d3250bf407f0579c8355cd39cdd4f9e1/37.7833,122.4167") {
if let data = NSData(contentsOfURL: url){
do {
let parsed = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)

let newDict = parsed as? NSDictionary
print(newDict!["currently"]!["summary"])
}
catch let error as NSError {
print("A JSON parsithng error occurred, here are the details:\n \(error)")
}
}
}
}

当我将其放入 swift 中时,它会自动将大部分代码更正为:

override func viewDidLoad() {
super.viewDidLoad()

if let url = URL(string: "https://api.forecast.io/forecast/d3250bf407f0579c8355cd39cdd4f9e1/42.2111,-88.3162") {
if let data = NSData(contentsOf: url){
do {
let parsed = try JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.allowFragments)

let newDict = parsed as? NSDictionary
print(newDict!["currently"]!["summary"])
}
catch let error as NSError {
print("A JSON parsithng error occurred, here are the details:\n \(error)")
}
}
}
}

我现在在 print(newDict!["currently"]!["summary"]) 行中收到错误,因为类型 'Any' 没有下标成员,我不确定如何要解决这个问题。删除!产生一个错误,指出必须有一个!还是一个?但是放任一个都会让我回到这个“任何”错误。非常感谢您提供任何帮助

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