ios - 类型 'MyWeather'不符合协议(protocol) 'Encodable'错误

标签 ios swift api

我正在尝试制作一个使用 OpenWeatherMap API 来检查当前天气的 iOS 应用程序,但我收到一条错误消息:

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

我是 Swift 编程新手,这可能是一个简单的错误。

我的代码如下:

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,所以仅当您更改 key 名称时才使用 CodingKeys,官方 json 的可编码为

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
}

关于ios - 类型 'MyWeather'不符合协议(protocol) 'Encodable'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51748629/

相关文章:

api - 获取已发送邮件的id

java - 登录

ios - 如何修复此 Xcode 错误 : "Warning unable to build chain to self signed root for signer apple Development"?

ios - 如何在NSPredicate中使用NSDate和NSString从Core Data中获取数据?

ios - 将重新排序图标设置为透明 UITableViewCells - moverowat 函数

swift - 将 UITableview 与订阅数据同步

ios - wcf服务发布数据IOS

ios - 升级到 Safari 7.0.4 后无法在本地和远程设置 Web 检查器断点

ios - 为什么它不能在 swift 2 的 Playground 上显示任何东西?

ruby - 处理 Braintree 超时的最佳方法