如何从OpenWeatherMap解析“雨”?

时间:2018-10-06 21:17:19

标签: swift xcode openweathermap

在从OpenWeatherMap API实现Rain时,我遇到了此错误:

'h'不是整数文字中的有效数字

我有一个单独的.swift文档来解析所有API引用,并尝试过这样解析Rain:

struct Rain: Decodable {
let 3h: Double?
}

但是我不确定如何解决“雨水:3小时”,因此不会发生错误。?

有人可以看看OpenWeatherMap API并让我知道他们的想法吗?

更新

struct.swift:

struct Rain: Decodable {
enum CodingKeys: String, CodingKey { case threeHours = "3h" }

let threeHours: Double?
}

ViewController.swift

@IBOutlet weak var precipitation: UILabel!


let clouding = (self.rain?.threeHours!)!
precipitation.text = precipitation.text! + "  " + String(format:"%.0f", clouding)

1 个答案:

答案 0 :(得分:1)

只需通过CodingKeys 翻译

struct Rain: Decodable {
    enum CodingKeys: String, CodingKey { case threeHours = "3h" }

    let threeHours: Double?
}

struct Rain: Codable {
    enum CodingKeys: String, CodingKey { case h3 = "3h" }

    let h3: Double?
}