简体   繁体   English

类型“ MyWeather”不符合协议“可编码”错误

[英]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''. 我正在尝试制作一个使用OpenWeatherMap API来检查当前天气的iOS应用,但是我收到一条错误消息,提示“类型'MyWeather'不符合协议“可编码”。 I am new to Swift Programming and it's probably a simple mistake. 我是Swift编程的新手,这可能是一个简单的错误。 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)

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 因为没有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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不符合“可编码”协议 - Type does not conform to protocol 'Encodable' 类型'X'不符合协议'Encodable' - Type 'X' does not conform to protocol 'Encodable' 类型“PlayerData”不符合协议“Decodable”和“Encodable” - Type 'PlayerData' does not conform to protocol 'Decodable' and 'Encodable' 类型“模型”不符合协议“可解码”/可编码 - Type 'Model' does not conform to protocol 'Decodable'/Encodable 不符合 Decodabel 和 Encodable 协议 - Does not conform to protocol Decodabel and Encodable 不符合协议 'Decodable' 和 'Encodable' - does not conform to protocol 'Decodable' and 'Encodable' 参数类型不符合可编码 - Argument type does not conform to Encodable swift 将字典转换为 jsonString 错误:协议类型 'Any' 不能符合 'Encodable' 因为只有具体类型才能符合协议 - swift Convert dictionary to jsonString error : Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols 错误:类型“ GearViewController”不符合协议“ UIPickerViewDataSource” - Error: Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM