ios - 类型“MyWeather”不符合协议“Encodable”错误
问题描述
我正在尝试制作一个使用 OpenWeatherMap API 来检查当前天气的 iOS 应用程序,但我收到一条错误消息,提示“类型 'MyWeather' 不符合协议 'Encodable'”。我是 Swift Programming 的新手,这可能是一个简单的错误。我会很感激任何帮助,谢谢。
我的代码如下:
struct MyWeather: Codable {
let name: String?
let location: String?
let temp: URL?
let wind: Int?
//THE NAMES OF THE JSON STUFF IN THE LINK
private enum CodingKeys: String, CodingKey {
case weather
case name
case location
case temp
case wind
//THE NAMES OF THE JSON STUFF IN THE LINK
}
}
class ViewController: UIViewController {
@IBAction func ShowWeatherInfo(_ sender: Any) {
guard let APIUrl = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=Crowland&appid=APIKEY&units=Metric") else { return }
URLSession.shared.dataTask(with: APIUrl) { (data, response
, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let weatherData = try decoder.decode(MyWeather.self, from: data)
解决方案
你需要删除这个
case weather
因为它没有 var 也CodingKeys
仅在您更改键名时才使用,因此官方 json 的 Codable 是
struct MyWeather: Codable {
let cod: String
let message: Double
let cnt: Int
let list: [List]
let city: City
}
struct City: Codable {
let id: Int
let name: String
let coord: Coord
let country: String
}
struct Coord: Codable {
let lat, lon: Double
}
struct List: Codable {
let dt: Int
let main: MainClass
let weather: [Weather]
let clouds: Clouds
let wind: Wind
let sys: Sys
let dtTxt: String
let rain, snow: Rain?
enum CodingKeys: String, CodingKey {
case dt, main, weather, clouds, wind, sys
case dtTxt = "dt_txt"
case rain, snow
}
}
struct Clouds: Codable {
let all: Int
}
struct MainClass: Codable {
let temp, tempMin, tempMax, pressure: Double
let seaLevel, grndLevel: Double
let humidity: Int
let tempKf: Double
enum CodingKeys: String, CodingKey {
case temp
case tempMin = "temp_min"
case tempMax = "temp_max"
case pressure
case seaLevel = "sea_level"
case grndLevel = "grnd_level"
case humidity
case tempKf = "temp_kf"
}
}
struct Rain: Codable {
let the3H: Double?
enum CodingKeys: String, CodingKey {
case the3H = "3h"
}
}
struct Sys: Codable {
let pod: Pod
}
enum Pod: String, Codable {
case d = "d"
case n = "n"
}
struct Weather: Codable {
let id: Int
let main: MainEnum
let description: Description
let icon: String
}
enum Description: String, Codable {
case brokenClouds = "broken clouds"
case clearSky = "clear sky"
case fewClouds = "few clouds"
case lightRain = "light rain"
case moderateRain = "moderate rain"
}
enum MainEnum: String, Codable {
case clear = "Clear"
case clouds = "Clouds"
case rain = "Rain"
}
struct Wind: Codable {
let speed, deg: Double
}
推荐阅读
- loops - 在循环外注册 xll 插件
- r - 从一个数据框列表中设置 colnames 以匹配另一个数据框列表
- python - Python Beautifulsoup 获取标签下的文本
- html - 有没有办法将 change.org 请愿嵌入网页?
- java - 我正在使用源和目标打印每个边缘,但是当我在 mxgraph java 中使用 getEdgeCount() 时它们会重复
- web - Web界面有通用术语吗?
- javascript - 我不明白,为什么我的 CalculateTotal() 函数返回“Null”?
- python - 如何从 python 的列表中删除 json 键?
- heroku - 推送被拒绝,Python App 失败到 Heroku
- python - 在 Python 中打开 excel 文件:XLRDError: Excel xlsx file; 不支持