我已阅读有关 SO 的主题,但尚未找到答案。 我一直在尝试使用天气 api 为我的应用程序下载天气数据。奇怪的是,我可以在没有“?”的网址上运行它但是这个 url 有一个 '?'内置。我怀疑这是问题所在,但我该如何解决它,或者让它忽略它呢?无论如何,那是我的理论。继承人的代码:
struct WeatherData: Decodable {
let description: String
let temp: Double
let wind: Double
}
func weather() {
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID={40b5f59a0004885043fe3df3e0b6ed8e}"
let urlObj = URL(string: url)
URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
do {
let weatherObj = try JSONDecoder().decode([WeatherData].self, from: data!)
print(data!)
for weather in weatherObj {
print(weather.temp, weather.description, weather.wind)
}
} catch {
print("Got an Error")
}
}.resume()
}
因此按原样运行会产生错误:“线程 1: fatal error :在展开可选值时意外发现 nil”或 URLSession 行。
我是否漏掉了一些非常明显的东西,或者是否有办法解决这个问题?
非常感谢
-- 更新: 因此,在更改结构和 {} 之后,它一直有效,直到我开始将数据输入标签。这是最新的尝试:
func weather() {
let lat = locationManager.location!.coordinate.latitude
let long = locationManager.location!.coordinate.longitude
//let baseURL = "http://api.openweathermap.org/data/2.5/weather?"
let apiKey = "40b5f59a0004885043fe3df3e0b6ed8e"
//let weatherURL = URL(string: "\(baseURL)lat=\(lat)&lon=\(long)&units=metric&APPID=\(apiKey)")
let weahterURL = "http://api.openweathermap.org/data/2.5/weather?lat=\(lat)&lon=\(long)&units=metric&APPID=\(apiKey)"
//let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID=40b5f59a0004885043fe3df3e0b6ed8e"
let urlObj = URL(string: weahterURL)
URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
do {
let weatherObj = try JSONDecoder().decode(WeatherData.self, from: data!)
print(weatherObj)
//seems as though not gettign any data from beyond this point
var desc = weatherObj.weather
var wind = weatherObj.wind.speed
var tempMin = weatherObj.main.temp_min
var tempMax = weatherObj.main.temp_max
DispatchQueue.main.async {
self.weatherDesc = desc
self.weartherWind.text = wind
self.tempMinTxt.text = tempMin
self.tempMaxTxt.text = tempMax
}
} catch {
print("Got an Error", error.localizedDescription)
}
}.resume()
}
最佳答案
您错误地构造了 url 而不是
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID={40b5f59a0004885043fe3df3e0b6ed8e}"
做
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID=40b5f59a0004885043fe3df3e0b6ed8e"
这个
{40b5f59a0004885043fe3df3e0b6ed8e}
应该是
40b5f59a0004885043fe3df3e0b6ed8e
此外您为解码器创建的结构无效,不会获取数据
//
struct WeatherData: Decodable {
let weather: [WeatherItem]
let wind: WindItem
let main : MainItem
}
//
struct WeatherItem: Decodable {
let description: String
}
//
struct WindItem: Decodable {
let speed: Double
let deg: Double
}
//
struct MainItem : Decodable {
let tempMin: Double
let tempMax: Double
private enum CodingKeys: String, CodingKey {
case tempMin = "temp_min" , tempMax = "temp_max"
}
}
//
let weatherObj = try JSONDecoder().decode(WeatherData.self, from: data!)
关于json - 带有 URL 的 URLSession 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51410014/