Type 'MyWeather' does not conform to protocol 'Encodable' error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Type 'MyWeather' does not conform to protocol 'Encodable' error



I'm trying to make an iOS app that uses the OpenWeatherMap API to check the current weather, but I'm getting an error saying 'Type 'MyWeather' does not conform to protocol 'Encodable''. I am new to Swift Programming and it's probably a simple mistake. I would appreciate any help, thank you.



My code below:


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)




1 Answer
1



You need to remove this


case weather



as there is no var for it also use CodingKeys only if you'll change key name , the Codable for the official json is


CodingKeys


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





Can I ask how you created these Structs from the API link? is there a website or program that can generate these Structs? Thanks
– Anthony
Aug 19 at 22:01






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard