【发布时间】:2018-12-03 22:21:45
【问题描述】:
我试图通过 JSONDecoder 解析 JSON 并使用 Alamofire 获取数据。但是,当我运行该应用程序时,它显示由于格式不正确而无法读取数据。我已经尝试了很多东西,但仍然没有工作。任何帮助,将不胜感激。来源如下:
VC:
class SecondTaskVC: UIViewController {
var weatherModel = [WeatherModelDecodable]()
override func viewDidLoad() {
let url = URL(string: "https://api.openweathermap.org/data/2.5/forecast?lat=42.874722&lon=74.612222&APPID=079587841f01c6b277a82c1c7788a6c3")
Alamofire.request(url!).responseJSON { (response) in
let result = response.data
do{
let decoder = JSONDecoder()
self.weatherModel = try decoder.decode([WeatherModelDecodable].self, from: result!) // it shows this line as a problem
for weather in self.weatherModel {
print(weather.city.name)
}
}catch let error{
print("error in decoding",error.localizedDescription)
}
}
}
}
数据模型:
struct WeatherModelDecodable: Decodable {
let city: CityDecodable
}
struct CityDecodable: Decodable {
let name: String
}
【问题讨论】:
标签: ios swift xcode alamofire decoder